Find Unused Resources in a .NET Solution

.NetResources

.Net Problem Overview


How do you go about finding unused icons, images, strings in .resx files that may have become 'orphaned' and are no longer required?

.Net Solutions


Solution 1 - .Net

Recently ResXManager 1.0.0.41 added a feature to show the number of references to a string resource.

Screenshot showing the new reference column

Solution 2 - .Net

I couldn't find any existing solution that would search for string resource references in XAML files and batch-remove unused ones.

So I wrote this: https://github.com/Microsoft/RESX-Unused-Finder

RESX Unused Finder screenshot

It searches a project directory for references to string resources, then displays a list of ones it couldn't find a match for. You can specify a template to search for so it can find references in XAML files.

Solution 3 - .Net

I created a free open-source VS extension that looks for unused images in a project, just published the first version: https://marketplace.visualstudio.com/items?itemName=Jitbit1.VSUnusedImagesFinder

Solution 4 - .Net

This is no information an algorithm can reliably compute. The inspected program could fetch a list of all resources and do something with them, like letting the user choose from several icons.

Your best bet is probably to search for all references to your resource-access API of choice and inspect those manually. Using grep/sed you might be able to reduce the sites you have to inspect manually by handling all "easy" ones where a simple string is used.

Solution 5 - .Net

Since I could not find a simple and fast solution yet, I found at least a solution that allows me to get the result I'm looking for, even if it takes some time (ideal for a lazy sunday afternoon).

The solution involves Visual Studio .NET 2010 and ReSharper (I'm using version 7.1) and goes like the following.

Step-by-step solution

1.) Right-click your primary RESX file in VS.NET and select "Find Usages" from the context menu:

enter image description here

This will bring up ReSharper's "Find Results" window.

2.) Double-click each occurrence in the solution window:

enter image description here

This will open the source code window with the resource.

3.) Rename this resource from within the source code window:

enter image description here

It will bring up ReSharper's "Rename Resource" dialog.

4.) Give the resource a new name with a unique prefix. In my example this is "TaskDialog_":

enter image description here

It will rename both the resource and also the auto-generated C# wrapper/access class.

5.) Repeat the above steps 2, 3 and 4 for all resources in the "Usages" window.

6.) Open the RESX file in Visual Studio's resource editor and select all files without the prefix:

enter image description here

7.) Now click the "Remove Resource" button on the top of the window or simply press the Del key:

enter image description here

You finally have a RESX file with only the acutally used resources in your file.

8.) (Optionally) If you have resources in multiple languages (e.g. "Resources.de.resx" for German), repeat steps 7 and 8 for those RESX files, too.

Warning

Please note that this will not work if you access your strings other than through the strongly-typed, auto-generated C# class Resources.

Solution 6 - .Net

I recently built a tool that detects and removes unused string resources. I used the information in this post as a reference. The tool may not be perfect, but it does the heavy-lifting part and will be useful if you have a big project with the long history. We used this tool internally to consolidate resource files, and remove unused resources (we got rid of 4,000+ resources out of 10,000).

You can look at the source code, or just install ClickOnce from here: https://resxutils.codeplex.com/

Solution 7 - .Net

I had a similar problem. Several thousand resource strings that I'd created for a translation table, many of which were no longer required or reference by code. With around 180 dependent code files, there was no way I was going to manually go through each resource string.

The following code (in vb.net) will go through your project finding orphaned resources (in the project resources, not any individual forms' resources). It took around 1 minute for my project. It can be modified to find strings, images or any other resource type.

In summary it;

    1. Uses the solution project file to gather all the included code modules and appends them into a single string variable;
    1. Loops through all the project resource objects, and creates a list (in my case) of those which are strings;
    1. Does a string search finding resource string codes in the combined project text variable;
    1. Reports resource objects that are not referenced.

The function returns the object names on the windows clipboard for pasting in a spreadsheet or as a list array of the resource names.

edit : example call in module : modTest
? modTest.GetUnusedResources("C:\Documents and Settings\me\My Documents\Visual Studio 2010\Projects\myProj\myProj.vbproj", True, true)

'project file is the vbproj file for my solution
Public Function GetUnusedResources(projectFile As String, useClipboard As Boolean, strict As Boolean) As List(Of String)


    Dim myProjectFiles As New List(Of String)
    Dim baseFolder = System.IO.Path.GetDirectoryName(projectFile) + "\"

    'get list of project files 
    Dim reader As Xml.XmlTextReader = New Xml.XmlTextReader(projectFile)
    Do While (reader.Read())
        Select Case reader.NodeType
            Case Xml.XmlNodeType.Element 'Display beginning of element.
                If reader.Name.ToLowerInvariant() = "compile" Then ' only get compile included files
                    If reader.HasAttributes Then 'If attributes exist
                        While reader.MoveToNextAttribute()
                            If reader.Name.ToLowerInvariant() = "include" Then myProjectFiles.Add((reader.Value))
                        End While
                    End If
                End If
        End Select
    Loop

    'now collect files into a single string
    Dim fileText As New System.Text.StringBuilder
    For Each fileItem As String In myProjectFiles
        Dim textFileStream As System.IO.TextReader
        textFileStream = System.IO.File.OpenText(baseFolder + fileItem)
        fileText.Append(textFileStream.ReadToEnd)
        textFileStream.Close()
    Next
    '  Debug.WriteLine(fileText)

    ' Create a ResXResourceReader for the file items.resx.
    Dim rsxr As New System.Resources.ResXResourceReader(baseFolder + "My Project\Resources.resx")
    rsxr.BasePath = baseFolder + "Resources"
    Dim resourceList As New List(Of String)
    ' Iterate through the resources and display the contents to the console.
    For Each resourceValue As DictionaryEntry In rsxr
        ' Debug.WriteLine(resourceValue.Key.ToString())
        If TypeOf resourceValue.Value Is String Then ' or bitmap or other type if required
            resourceList.Add(resourceValue.Key.ToString())
        End If
    Next
    rsxr.Close()  'Close the reader.

    'finally search file string for occurances of each resource string
    Dim unusedResources As New List(Of String)
    Dim clipBoardText As New System.Text.StringBuilder
    Dim searchText = fileText.ToString()
    For Each resourceString As String In resourceList
        Dim resourceCall = "My.Resources." + resourceString ' find code reference to the resource name
        Dim resourceAttribute = "(""" + resourceString + """)" ' find attribute reference to the resource name
        Dim searchResult As Boolean = False
        searchResult = searchResult Or searchText.Contains(resourceCall)
        searchResult = searchResult Or searchText.Contains(resourceAttribute)
        If Not strict Then searchResult = searchResult Or searchText.Contains(resourceString)
        If Not searchResult Then ' resource name no found so add to list
            unusedResources.Add(resourceString)
            clipBoardText.Append(resourceString + vbCrLf)
        End If
    Next

    'make clipboard object
    If useClipboard Then
        Dim dataObject As New DataObject ' Make a DataObject clipboard
        dataObject.SetData(DataFormats.Text, clipBoardText.ToString())        ' Add the data in string format.
        Clipboard.SetDataObject(dataObject) ' Copy data to the clipboard.
    End If

    Return unusedResources

End Function

Solution 8 - .Net

I use ReSharper for finding unused resource fields and then remove them manually if project contains small amount of resources. Some short script can be used if we already have list of unused items.

The solution is next:

  • show all unused members as described in this article
  • temporary remove *.Designer.cs from Generated file masks (ReSharper → Options → CodeInspection → GeneratedCode)
  • Also comment or remove comment (that indicates that code is auto generated) from top of Designer.cs file attached to resource file.

You will have list of all unused resources, left to remove them from resx.

Solution 9 - .Net

I've been considering this myself and I believe I have two options. Both of these rely on the fact that I use a helper method to extract the required resource from the resource files.

  1. Logging
    Add some code to the "getresource" method or methods so that every time a resource is accessed, the resource key is written to a log. Then try to access every part of the site (a testing script might be helpful here). The resultant log entries should give a list of all the active resource keys, the rest can be junked.

  2. Code Analysis
    I am looking at whether [T4][1] is capable of working through the solution and creating a list of all references to the "getresource" helper method. The resultant list of keys will be active, the rest can be deleted.

[1]: http://msdn.microsoft.com/en-us/library/bb126445.aspx "T4"

There are limitations of both methods. The logging method is only as good as the code covered by the test and the code analysis might not always find keys rather than strings containg the keys so there will be some extra manual work required there.

I think I'll try both. I'll let you know how it goes.

Solution 10 - .Net

Rename your current image directory and then create a new one, do a find-in-files search within VS for your image path, i.e. '/content/images', multiselect all the used images and drag them into the new image folder. You can then exclude the old directory from the project, or just delete it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmrpbodyView Question on Stackoverflow
Solution 1 - .NethultqvistView Answer on Stackoverflow
Solution 2 - .NetRandomEngyView Answer on Stackoverflow
Solution 3 - .NetAlex from JitbitView Answer on Stackoverflow
Solution 4 - .NetDavid SchmittView Answer on Stackoverflow
Solution 5 - .NetUwe KeimView Answer on Stackoverflow
Solution 6 - .NetvotrubacView Answer on Stackoverflow
Solution 7 - .NetCraig StewartView Answer on Stackoverflow
Solution 8 - .NetAnatoliyView Answer on Stackoverflow
Solution 9 - .NetChris SimpsonView Answer on Stackoverflow
Solution 10 - .NetDevHeadView Answer on Stackoverflow