views:

1339

answers:

10

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

A: 

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

Darren Kopp
+3  A: 

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

Chris Canal
A: 

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

Jekke
+2  A: 

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.

Craig
+1  A: 

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.

Russell Myers
A: 

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.

Sam Erwin
+3  A: 

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.

Graeme Bradbury
+2  A: 

(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...

Benjol
+3  A: 

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.

Gabe
this is also available in Visual Studio 2010
Junior Mayhé
+1  A: 

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.

jbe
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