Organize Usings Across Your Entire Solution

One of the features I worked on for the Visual Studio 2008 release was the new "Organize Usings" feature.  This feature allows you to:

  1. Remove Unused Usings - Determines which using directives are not used in the current file and deletes them.   
  2. 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 ( https://blogs.msdn.com/kevinpilchbisson/archive/2004/05/17/133371.aspx ) and Chris Eargle ( https://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.  

 Public Module Module1
     Sub OrganizeSolution()
         Dim sol As Solution = DTE.Solution
         For i As Integer = 1 To sol.Projects.Count
             OrganizeProject(sol.Projects.Item(i))
         Next
     End Sub
  
     Private Sub OrganizeProject(ByVal proj As Project)
         For i As Integer = 1 To proj.ProjectItems.Count
             OrganizeProjectItem(proj.ProjectItems.Item(i))
         Next
     End Sub
  
     Private Sub OrganizeProjectItem(ByVal projectItem As ProjectItem)
         Dim fileIsOpen As Boolean = False
         If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
             'If this is a c# file 
             If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                 'Set flag to true if file is already open 
                 fileIsOpen = projectItem.IsOpen
                 Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                 window.Activate()
                 projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")
                 'Only close the file if it was not already open 
                 If Not fileIsOpen Then
                     window.Close(vsSaveChanges.vsSaveChangesYes)
                 End If
             End If
         End If
         'Be sure to apply RemoveAndSort on all of the ProjectItems. 
         If Not projectItem.ProjectItems Is Nothing Then
             For i As Integer = 1 To projectItem.ProjectItems.Count
                 OrganizeProjectItem(projectItem.ProjectItems.Item(i))
             Next
         End If
         'Apply RemoveAndSort on a SubProject if it exists. 
         If Not projectItem.SubProject Is Nothing Then
             OrganizeProject(projectItem.SubProject)
         End If
     End Sub
 End Module

Step 3: Run the macro on any solution that you'd like and there you have it!  Enjoy :)