一次删除整个项目或解决方案中未使用的命名空间 [英] Remove unused namespaces across a whole project or solution at once

查看:102
本文介绍了一次删除整个项目或解决方案中未使用的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以逐个文件地做.

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?

推荐答案

你的意思是使用语句吗?首先,请注意,它们通常不会伤害其他占用空间的东西.ReSharper 等工具提供了自动执行此操作的技巧,但是:有一个 链接到VS提要一会儿前;归结为:

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:

  • 转到工具 -> 宏 -> 宏 IDE...
  • 在 Project Explorer 中,添加 -> 添加模块...(输入名称 - 我使用了 OrganiseUsings)
  • 粘贴下面的代码
  • 文件 -> 保存 MyMacros,退出

现在,如果您右键单击工具栏并自定义... - 您应该能够找到 MyMacros.OrganiseUsings.RemoveAndSortAll - 将其拖到方便的地方(可能是工具菜单;您可能还想在放置后更改名称它).

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

这篇关于一次删除整个项目或解决方案中未使用的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆