How do i automate adding a “using” statement to every files in a folder, namespace or project with Visual Studio 2005 / resharper

守給你的承諾、 提交于 2019-12-01 16:07:54

I'd try a regex in the "Find and Replace" dialog:

Replace

^using System;$

with

using System;\nusing xxx;

This works only for files using the System namespace, but maybe you find another common namespace or structure element. After doing so you can refactor all files in your solution(/folder) with the resharper. That will remove doubled usings.

Update: Did you introduce new namespaces for existing types? There is a refactor function called "move". It will move your type to a new namespace and preserve the references.

  • Open ReSharper Options / Languages / C# / Namespace Imports
  • Add "Namespaces that should always be imported"
  • Run Code Cleanup against solution or project. You may want to create profile (Options / Tools / Code Cleanup) with only Optimize Using Directives module.

VS will add them for you. When you add a symbol in a referenced assembly, but without a using statement for the symbol, you will get a marker against the symbol. Press control-period (or use the mouse) and the first option will add the using statement for you.

Otherwise you could write a VS macro to open each project source file in turn and insert the statement.

When you encounter a file, one-by-one, press CTRL+ALT+SHIFT+F for the automated file cleanup routine. It just takes a second to run, and will do what you're looking for, but not just for System.

not sure if R# has a way to do solution wide file cleanups.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!