Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: Detecting unneeded Assemblies

On larger and/or long running projects, I tend to reference many assemblies and namespaces, and often I end up removing some functionality later on or moving it into a different project.

I just wonder, is there a way to check every project (heck, every .cs file) in my whole Visual Studio solution and get a list of all referenced Assemblies and Namespaces that are not actually being used and can be safely removed? I know that ReSharper can do it for a single Code File, but I did not see an option to check all files or to check for unneeded Assemblies.

Using Visual Studio 2005 and 2008 Professional if that matters.

Edit: Thanks so far. The Problem with ReSharper or "Remove and Readd if build breaks" is that it's quite tedious on every single file and assembly (My project has about 120 .cs Files in 7 Assemblies, and references a total of 18 Assemblies outside of the solution), so ideally i'm looking for something "one-click". Big Bonus points for some automatic way that can be used in buildscripts to generate a report :)

like image 233
Michael Stum Avatar asked Sep 18 '08 19:09

Michael Stum


People also ask

How do I get rid of unused Visual Studio?

CTRL + K + E Will sort and remove your unused usings.

How do I delete unused namespaces in Visual Studio 2022?

Navigate to Tools > Options > Text Editor > Code Cleanup. Add a check in the "Run Code Cleanup profile on Save." Be sure to select the appropriate profile you want to execute automatically whenever you save!

How do I know if my NuGet package is unused?

Use ReSharper or similar code analysis tool to identify any unused references in your projects and uninstall the nuget in the corresponding projects. Sometimes uninstalled nugets still linger in the Installed packages and Updates lists in the Manage NuGet Packages dialog.


2 Answers

Removing unused references is a feature Visual Studio 2008 already supports. Unfortunately, only for VB .NET projects.

I have opened a suggestion on Microsoft Connect to get this feature for C# projects too:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=510326

If you like this feature as well then you might vote my suggestion.

like image 77
jbe Avatar answered Nov 16 '22 03:11

jbe


I found this question while searching in Google and looking for a way to remove unused "Using" statements from my code.

Refactor is great, but it isn't available to me. However, as it turns out Visual Studio 2008 does this on its own.

Here are the steps:

  1. Load the code in Visual Studio.
  2. Right-click anywhere on the code page.
  3. Click "Organize Usings" from the menu.
  4. Click "Remove Unused Usings"

Done. I realize this doesn't answer the original question (doing this en masse for an entire project) but it does answer my question.

like image 33
Gabe Sumner Avatar answered Nov 16 '22 02:11

Gabe Sumner