Meeting the needs of your business from a distance

Organize Usings Across Your Entire Solution

by Mark Shiffer 8/25/2008 12:00:00 PM

Macro to organize (remove and sort) using clauses in Visual Studio. (Original Source)

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

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Tools | Programming

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

January 6. 2009 17:13

About the author

Name of author Mark Shiffer
CEO & CIO of MS Consulting

E-mail me Send mail

Calendar

<<  January 2009  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Pages

    Recent posts

    Recent comments

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2009

    Sign in

    Copyright © 2001-2009 MS Consulting, Inc. All Rights Reserved.