一次删除整个项目或解决方案中未使用的命名空间

Remove unused namespaces across a whole project or solution at once(一次删除整个项目或解决方案中未使用的命名空间)
本文介绍了一次删除整个项目或解决方案中未使用的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)