C# 一次性删除整个项目或解决方案中未使用的命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/283471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove unused namespaces across a whole project or solution at once
提问by leora
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?
有没有什么办法可以一次性为项目中的所有文件做到这一点?
采纳答案by Marc Gravell
Do you mean using statements? First, note that they generally do no harm other that take space. Tools like ReSharperoffer automated tricks to do this, however: there was a link in the VS feeda little while ago; it boils down to:
你的意思是使用语句?首先,请注意,它们通常不会对占用空间的其他人造成伤害。然而,像ReSharper这样的工具提供了自动化的技巧来做到这一点:不久前在 VS 提要中有一个链接;归结为:
- 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
- 转到工具 -> 宏 -> 宏 IDE...
- 在项目资源管理器中,添加 -> 添加模块...(输入名称 - 我使用过 OrganiseUsings)
- 粘贴下面的代码
- 文件 -> 保存 MyMacros,退出
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).
现在,如果您右键单击工具栏并自定义... - 您应该能够找到 MyMacros.OrganiseUsings.RemoveAndSortAll - 将其拖到方便的地方(可能是工具菜单;您可能还想在放置后更改名称)。
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
回答by Mesh
If you do mean 'using' Power Commands contains this functionality + a boat load more.
如果您的意思是“使用” Power Commands 包含此功能 + 船载更多。
回答by vmachacek
For Visual Studio 2010 you can download the "Remove and Sort Using"extension from the Visual Studio Gallery.
对于 Visual Studio 2010,您可以从 Visual Studio Gallery 下载“Remove and Sort Using”扩展。
http://visualstudiogallery.msdn.microsoft.com/en-us/cb559aa8-d976-4cc2-9754-5a712f985d16
http://visualstudiogallery.msdn.microsoft.com/en-us/cb559aa8-d976-4cc2-9754-5a712f985d16
Works Well for me
对我来说效果很好
回答by mghaoui
Here's a small improvement on the script above for VB.NET. Make sure you have the Productivity Power Toolsinstalled.
这是对上述 VB.NET 脚本的一个小改进。确保您已安装Productivity Power Tools。
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
回答by VivekDev
Productivity Power tools is what you need. https://visualstudiogallery.msdn.microsoft.com/dbcb8670-889e-4a54-a226-a48a15e4cace
生产力 电动工具正是您所需要的。 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.
安装后,您可以从“工具 –> 选项 –> 生产力电动工具 –> PowerCommands –> 将军”中找到“在保存时删除和排序使用”。选中该选项后,重新启动 VS。现在保存,你会看到魔法。
For VS 2015, take a look at this
对于 VS 2015,看看这个
回答by Mark Bell
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 and 2017:
参考 Productivity Power Tools 扩展的其他答案没有详细说明如何实际执行此操作,因此这里是 Visual Studio 2013、2015 和 2017 的一些说明:
First, open the Tools > Extensions and Updates...dialog in Visual Studio, select Onlinein the left-hand bar and then search the Visual Studio Gallery for "Productivity Power Tools". Install the extension and restart VS.
首先,在 Visual Studio 中打开Tools > Extensions and Updates...对话框,在左侧栏中选择Online,然后在 Visual Studio Gallery 中搜索“Productivity Power Tools”。安装扩展并重新启动VS。
Alternatively, you can manually download and install the extensions for your version of Visual Studio:
或者,您可以手动下载并安装适用于您的 Visual Studio 版本的扩展:
Productivity Power Tools 2013
Productivity Power Tools 2015
Productivity Power Tools 2017
生产力电动工具 2013
生产力电动工具 2015
生产力电动工具 2017
For VS2017, you can also download the Power Commands extension separately from the others in the Power Tools pack:
对于 VS2017,您还可以从 Power Tools 包中的其他扩展单独下载 Power Commands 扩展:
Power Commands for Visual Studio (VS2017)
Be aware that at the time of writing, the VS2017 version doesn't work with .Net Core projects/solutions.
请注意,在撰写本文时,VS2017 版本不适用于 .Net Core 项目/解决方案。
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 beforerunning it, so that you can revert the changes it makes if something goes wrong!
这可能需要一段时间,尤其是在大型解决方案上;它也不会让修改后的文件保持打开状态(因此无法撤消),因此请确保在运行之前提交所选 VCS 中的所有内容,以便在出现问题时恢复它所做的更改!
Update: Format All Files
更新:格式化所有文件
Recently I've been using the Format All Filesextension, which allows you to execute Format Document, Remove and Sort Usingsand one other custom command of your choice (all optionally, set in the VS preferences).
最近我一直在使用Format All Files扩展,它允许您执行Format Document、Remove 和 Sort Usings以及您选择的另一个自定义命令(所有这些都可选,在 VS 首选项中设置)。
It seems to work very well, but again, no undo, so make sure you commit everything in your VCS of choice beforerunning it.
它似乎工作得很好,但同样无法撤消,因此请确保在运行之前提交所选 VCS 中的所有内容。
回答by Tim Pickin
I am using Visual Studio 2015 and found a tool named BatchFormat: https://marketplace.visualstudio.com/items?itemName=vs-publisher-147549.BatchFormat
我正在使用 Visual Studio 2015 并找到一个名为 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.
您选择的任何内容都会应用于解决方案中的每个文件,正如您在屏幕截图中所见,还有其他选项,您还可以设置每个文档的格式。
回答by dave thieben
for a more recent version, including 2017, try the "Format All Files"extension. it has been working really well for me.
对于更新的版本,包括 2017,请尝试“格式化所有文件”扩展名。它对我来说效果很好。
回答by sotn
There is no need for any plugins in VS 2017. Click the bulb icon near any using statement and click Solution
next to Fix all occurrences in
part.
VS 2017 中不需要任何插件。单击任何 using 语句附近的灯泡图标,然后单击零件Solution
旁边的Fix all occurrences in
。