13

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 :)

3
  • Would be nice if the question was in terms of free tools, not just tools. I happen to have resharper so the solution below is good, but still...
    – Oskar
    Sep 18, 2008 at 20:07
  • Resharper recently made a cheaper personal license. I think it's 134 € for C# now. Free tools would be nice of course (if MSBuild could do it that would by honkey-dorey!), but i'm mainly looking for something that works first :) Sep 18, 2008 at 20:10
  • Related: stackoverflow.com/questions/81597/…
    – Josh Lee
    Nov 13, 2009 at 14:09

10 Answers 10

4

Resharper will do this for you and you can set it up in the Clean Code option that you can run solution wide ;o)

4

If you have ReSharper then, select the solution, right click and select cleanup code. Resharper will then go through every code file in the solution.

As to removing project references, when the compiler runs it won't add the reference if no code uses that dll.

3

If you have ReSharper installed, then from within the Solution Explorer you can right-click on a reference and click Find Dependent Code. If it comes back with a dialog of results then you're using that reference/assembly. If you get the message "Code dependent on module module name not found." Then you should be OK to remove that reference/assembly because it's not being used.

2

(Cross-posted from here) Given that VisualStudio (or is it msbuild?) detects unused references and doesn't include them in the output file, you can write a script which parses the references out of the csproj, and compares that with the referenced Assemblies detected by reflexion on the project output.

If you're motivated...

2

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.

1
  • this is also available in Visual Studio 2010 Jun 4, 2010 at 21:25
2

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.

1
  • Thanks for opening up the suggestion on connect, not enough people put the effort forth to do so. Also I'm wondering if there is anyway to get at this hidden feature through use of a Macro for a C# project.
    – jpierson
    May 26, 2010 at 19:04
1

As was mentioned by previous responses, Resharper works well for this as well as a variety of other cases such as ways to make code cleaner and generate certain things for you. The best way to really figure out all the advantages of Resharper is to download the trial, print out a Keymap Cheatsheet and stick it next to you where you're developing at.

Long story short, it does solve this problem but it does a heck of a lot more than just that.

0

Any decent code profiler will do this for you. I like DevPartner personally.

0

It won't get rid of any references in code to your referenced assemblies (I think), but you can go to the properties of each of your projects, got to References, and click "Unused References...". Visual Studio then gives you the option to remove them right there.

No way to do it at the solution level though.

-1

Yeah, i don't think there is one. I just delete some i don't think are needed then build :/

BTW. that will keep using errors. You can use the Visual Studio Power commands to remove and sort usings, so do that first, then delete random assemblies :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.