项目范围“删除和排序使用”

时间:2009-07-10 00:19:13

标签: visual-studio-2008

是否有人编写了一个宏,可以在整个项目中删除和排序您的使用?我想在没有像ReSharper这样的第三部分加载项的情况下完成此任务。

1 个答案:

答案 0 :(得分:3)

如果您考虑使用 Microsoft 加载项,您会发现Power Commands实用程序会为项目或解决方案执行此操作。

更新:我知道我有一些代码可以做到这样的事情,我找到了它,现在它是:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module FormatDocuments

    Public Sub FormatSolution()

        Dim sol As Solution = DTE.Solution

        For i As Integer = 1 To sol.Projects.Count

            FormatProject(sol.Projects.Item(i))

        Next


    End Sub

    Private Sub FormatProject(ByVal proj As Project)
        For i As Integer = 1 To proj.ProjectItems.Count

            FormatProjectItem(proj.ProjectItems.Item(i))

        Next


    End Sub

    Private Sub FormatProjectItem(ByVal projectItem As ProjectItem)

        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then

            If projectItem.Name.EndsWith(".cs") Then

                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)

                window.Activate()

                projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")

                window.Close(vsSaveChanges.vsSaveChangesYes)

            End If
        End If

        If projectItem.ProjectItems.Count > 0 Then
            For Each subProjectItem As ProjectItem In projectItem.ProjectItems
                FormatProjectItem(subProjectItem)
            Next
        End If
    End Sub


End Module