views:

309

answers:

3

One of the things I love about Visual Studio 2008 is the ability to refactor and reorganize the "using" statements in source code files (this may have been in 2005 as well, I don't remember).

Specifically, I'm talking about how you can have it both reorganize the statements to be alphabetical (though with the core FCL libraries floating to the top) and removing any statements which don't need to be there (either never used or no longer used).

Is there any way to automate this refactoring (the sorting and trimming) across an entire old codebase? Either through all of the files in a solution or across multiple solution files.

+6  A: 

I believe you can do it solution wide using Power Commands

From PowerCommands Documentation:

Remove and Sort Usings This command removes and sort using statements for all classes given a project. It is useful, for example, in removing or organizing the using statements generated by a wizard. This command can be executed from a solution node or a single project node.

Quintin Robinson
Couldn't have answered it better myself.
Pat
I LOVE POWER COMMANDS!!!!!
David Basarab
Oh wow I just showed this around the office and now I'm like the hero of the day. Thanks!
Schnapple
Glad everyone is happy!
Quintin Robinson
+3  A: 

ReSharper is a (commercial) Visual Studio plugin that has a "Code Cleanup" utility than can be run at a solution-wide level. The utility detects and removes unreferenced using statements, and performs other useful maintenance. I am unsure if it provides functionality to sort them, however.

Mike
I'm not sure if it does by default, but if it doesn't the StyleCop plugin for it definitely does.
Lawrence Johnston
+1  A: 

Use PowerCommands addon for Visual Studio '08. You can right click the solution in the Solution Explorer and choose "Remove And Sort" usings. Also, there are options in the Tools-->Options dialog box (scroll down to the Power Commands item) for removing on save and reformatting your code ( using vs settings ) on save.

Muad'Dib