Finding Unused Resources in a .NET Solution

How can you find unused icons, images, lines in .resx files that may have become "lost" and are no longer needed?

+48
resources
Oct 29 '08 at 6:27
source share
10 answers

This is not information that the algorithm can reliably calculate. A proven program can get a list of all resources and do something with them, for example, let the user select several icons.

It is probably best to find all the links to your resource access APIs and check them manually. Using grep / sed , you can reduce the number of sites that you must check manually using all the "simple" ones that use a simple string.

+10
Oct 29 '08 at 7:13
source share

Recently ResXManager 1.0.0.41 added a function that shows the number of links to a string resource.

Screenshot showing the new reference column

+28
Sep 04 '14 at 13:12
source share

I could not find any existing solution that would look for links to string links in XAML files and batch delete unused ones.

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

RESX Unused Finder screenshot

It searches for the project directory for links to string resources, and then displays a list of those that could not be found. You can specify a search pattern so that it can find links in XAML files.

+13
Oct 27 '14 at 23:41
source share

I created a free open source VS extension that searches for unused images in a project, just posted the first version: https://github.com/jitbit/vs-unused-image-finder

+10
Mar 23 '12 at 7:40
source share

Since I could not find a simple and quick solution, I at least found a solution that allows me to get the result that I am looking for, even if it takes some time (ideal for a lazy Sunday).

The solution includes Visual Studio .NET 2010 and ReSharper (I am using version 7.1) and is as follows.

Step by step solution

1.) Right-click your main .resx file in VS.NET and select "Find Usage" in the context menu:

enter image description here

The ReSharper Search Results window appears.

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

enter image description here

This will open the source code window with the resource.

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

enter image description here

The ReSharper Rename Resource dialog box appears.

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 the automatically generated shell class / C # class.

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

6.) Open the RESX file in the Visual Studio Resource Editor and select all files without a prefix:

enter image description here

7.) Now click on the "Delete Resource" button at the top of the window or just press the Del key:

enter image description here

You finally have a .resx file with only active resources in your file.

8.) (optional) If you have resources in several languages ​​(for example, "Resources.de.resx" for German), repeat steps 7 and 8 for these RESX files too.

Attention

Please note that this will not work if you get access to your lines, except through a strongly typed, automatically generated C # Resources class.

+6
Dec 09
source share

I recently created a tool that detects and removes unused string resources. I used the information in this post as a link. The tool may not be perfect, but it does the hard work and will be useful if you have a large project with a long history. We used this tool to consolidate resource files and remove unused resources (we got rid of 4,000 other resources out of 10,000).

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

+5
Oct 28 '13 at 19:19
source share

I had a similar problem. Several thousand rows of resources that I created for the translation table, many of which are no longer required or code links. With approximately 180 dependent code files, I could not manually go through each line of the resource.

The following code (in vb.net) will go through your project to search for orphan resources (in the resources of the project’s resources , and not on any separate forms). It took about 1 minute for my project. It can be changed to find strings, images, or any other type of resource.

Finally:

  • 1) Uses the solution project file to collect all the included code of the modules and adds them to one string variable;
  • 2) Iterates over all objects of the project resource and creates a list (in my case) of those that are strings;
  • 3) Does the string search search for the string of the resource string in the combined text variable of the project;
  • 4) Reports resource objects that are not referenced.

The function returns the names of objects in the Windows clipboard for pasting into a spreadsheet or as an array of a list of 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 
+3
Feb 13 '13 at 22:36
source share

I use ReSharper to find unused resource fields and then delete them manually if the project contains a small amount of resources. Some short script can be used if we already have a list of unused elements.

The solution is as follows:

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

+2
Jun 13 '14 at 8:33
source share

I myself considered this, and I believe that I have two options. Both of them rely on the fact that I use a helper method to extract the required resource from resource files.

  • entrance
    Add code to the getresource method or methods so that every time a resource is accessed, the resource key is written to the log. Then try to access each part of the site (a testing script may be useful here). As a result, the log entries should contain a list of all active keys of the resource, the rest can be deleted.

  • Code analysis
    I see T4 is able to work through the solution and create a list of all references to the "getresource" helper method. The resulting list of keys will be active, the rest can be deleted.

There are limitations to both methods. The logging method is as good as the code covered by the test, and code analysis may not always find the keys, not the lines containing the keys, so some additional manual work will be required there.

I think I'll try both. I will let you know how this happens.

+1
Jun 30 '09 at 19:01
source share

Rename your current image directory, and then create a new one, search for search files in VS for your image path, that is, "/ content / images", select all the images used and drag them into the new image folder. You can then exclude the old directory from the project or simply delete it.

+1
Feb 03 2018-11-22T00:
source share



All Articles