Remove unused namespaces across a whole project or solution at once

前端 未结 10 1887
夕颜
夕颜 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 09:52

    There is no need for any plugins in VS 2017 or 2019. Click the bulb icon near any using statement and click Solution next to Fix all occurrences in part.

    0 讨论(0)
  • 2020-12-25 09:52

    for a more recent version, including 2017, try the "Format All Files" extension. it has been working really well for me.

    0 讨论(0)
  • 2020-12-25 09:53

    Productivity Power tools is what you need. https://visualstudiogallery.msdn.microsoft.com/dbcb8670-889e-4a54-a226-a48a15e4cace

    Once you have that installed, you can find the “Remove and Sort Usings on Save” from the “Tools –> Options –> Productivity Power Tools –> PowerCommands –> Generals”. After you check that option, restart VS. Now save and you see the magic.

    For VS 2015, take a look at this

    0 讨论(0)
  • 2020-12-25 09:54

    I am using Visual Studio 2015 and found a tool named BatchFormat: https://marketplace.visualstudio.com/items?itemName=vs-publisher-147549.BatchFormat

    This did the job perfectly.

    Install the tool, then right click on your solution in the solution explorer, then at the top of the menu you see batch format:

    Whatever you select is applied to every file in your solution, as you can see in the screenshot, there are other options, you can also format every document.

    0 讨论(0)
  • 2020-12-25 09:57

    Do you mean using statements? First, note that they generally do no harm other that take space. Tools like ReSharper offer automated tricks to do this, however: there was a link in the VS feed a little while ago; it boils down to:

    • go to Tools -> Macros -> Macros IDE...
    • in the Project Explorer, Add -> Add Module... (put in a name - I've used OrganiseUsings)
    • paste over with the code below
    • File -> Save MyMacros, exit

    Now if you right-click on the toolbar and Customize... - you should be able to find MyMacros.OrganiseUsings.RemoveAndSortAll - drag this somewhere handy (maybe the Tools menu; you might also want to change the name after placing it).

    You can now use this option to run the Remove and Sort command for an entire solution. A big time-saver.

    ==== code ====

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports System.Diagnostics
    
    Public Module OrganiseUsings
    
        Public Sub RemoveAndSortAll()
            On Error Resume Next
            Dim sol As Solution = DTE.Solution
    
            For i As Integer = 1 To sol.Projects.Count    
                Dim proj As Project = sol.Projects.Item(i)    
                For j As Integer = 1 To proj.ProjectItems.Count    
                    RemoveAndSortSome(proj.ProjectItems.Item(j))    
                Next    
            Next    
        End Sub    
    
        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)
                End If    
            End If    
    
            For i As Integer = 1 To projectItem.ProjectItems.Count    
                RemoveAndSortSome(projectItem.ProjectItems.Item(i))    
            Next
        End Sub   
    
    End Module
    
    0 讨论(0)
  • 2020-12-25 09:57

    If you do mean 'using' Power Commands contains this functionality + a boat load more.

    http://code.msdn.microsoft.com/PowerCommands

    0 讨论(0)
提交回复
热议问题