Remove unused namespaces across a whole project or solution at once

前端 未结 10 1888
夕颜
夕颜 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:00

    visual studio 2017 having inbuild feature to remove unnecessary name space from whole project.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-25 10:11

    The other answers which refer to the Productivity Power Tools extensions don't go into any detail of how to actually do this, so here are some instructions for Visual Studio 2013, 2015, 2017 and 2019:

    First, open the Tools > Extensions and Updates... dialog in Visual Studio, select Online in the left-hand bar and then search the Visual Studio Gallery for "Productivity Power Tools". Install the extension and restart VS.

    Alternatively, you can manually download and install the extensions for your version of Visual Studio:

    Productivity Power Tools 2013
    Productivity Power Tools 2015
    Productivity Power Tools 2017/2019

    For VS2017 and VS2019, you can also download the Power Commands extension separately from the others in the Power Tools pack:

    Power Commands for Visual Studio

    Be aware that at the time of writing, the VS2017 version doesn't work with .Net Core projects/solutions.

    Once you have the extension installed, just right-click the solution in Solution Explorer, then select Power Commands > Remove and Sort Usings.

    This can take a while, particularly on large solutions; it also doesn't keep modified files open (hence no undo), so make sure you commit everything in your VCS of choice before running it, so that you can revert the changes it makes if something goes wrong!

    Update: Format All Files

    Recently I've been using the Format All Files extension, which allows you to execute Format Document, Remove and Sort Usings and one other custom command of your choice (all optionally, set in the VS preferences).

    It seems to work very well, but again, no undo, so make sure you commit everything in your VCS of choice before running it.

    0 讨论(0)
  • 2020-12-25 10:13

    For Visual Studio 2010 you can download the "Remove and Sort Using"extension from the Visual Studio Gallery.

    http://visualstudiogallery.msdn.microsoft.com/en-us/cb559aa8-d976-4cc2-9754-5a712f985d16

    Works Well for me

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