Remove unused namespaces across a whole project or solution at once

前端 未结 10 1939
夕颜
夕颜 2020-12-25 09:46

I know you can do it file by file.

Is there any way to do this in one step for all files in a project?

10条回答
  •  春和景丽
    2020-12-25 10:02

    Here's a small improvement on the script above for VB.NET. Make sure you have the Productivity Power Tools installed.

        Private Sub RemoveAndSortSome(ByVal projectItem As ProjectItem)
        On Error Resume Next
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
            If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
    
                window.Activate()
    
                projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")
    
                window.Close(vsSaveChanges.vsSaveChangesYes)
    
            ElseIf projectItem.Name.LastIndexOf(".vb") = projectItem.Name.Length - 3 Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
    
                window.Activate()
    
                projectItem.Document.DTE.ExecuteCommand("EditorContextMenus.CodeWindow.OrganizeImports.RemoveandSortImports")
    
                window.Close(vsSaveChanges.vsSaveChangesYes)
            End If
        End I
    

提交回复
热议问题