One of the features I worked on for the Visual Studio 2008 release was the new "Organize Usings" feature. This feature allows you to:
- Remove Unused Usings - Determines which using directives are not used in the current file and deletes them.
- Sort Usings - Sorts the using directives in a file
We've received a great response from customers on the usefulness of this feature but one of the questions that I've received quite often is - "is there a way to make the feature work on an entire project or solution"? The current answer to this is no. Given the number of times I've heard the request though, I thought it would be worth posting a workaround that uses macros.
So here's what you need to do - it's really quite simple:
Step 1: Create a new macro in Visual Studio through the Tools | Macros menu.
Step 2: Paste the code below into the Module and save it
Note that the code below is courtesy of Kevin Pilch-Bisson (http://blogs.msdn.com/kevinpilchbisson/archive/2004/05/17/133371.aspx) and Chris Eargle (http://www.chriseargle.com/post/Format-Solution.aspx), who have posted macros that allow you to format across an entire solution. I'm simply re-applying their approach to invoke the RemoveAndSort command rather than the FormatDocument command. Note that I haven't tested this macro extensively so please use at your own risk.
Step 3: Run the macro on any solution that you'd like and there you have it! Enjoy 🙂