views:

209

answers:

4

I am using resharper to do a big refactoring and i keep going from file to file and adding the same namespace over and over again in the "using" section

is there anyway to add a "using" statement to every single file in a folder, namespace or project? Even though some files wont need the reference, most do, so it will save lots of time.

+2  A: 

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.

Richard
This is the best option I think. To expand - changing the default shortcut key to something more easy to type (e.g. ALT + down arrow) makes adding the usings as painless as possible.
Dan C.
but that is only one file at a time . . i want to do multiple files at once
ooo
Alt + down arrow is easier to type than Control + period?
Kyralessa
@mek: How many files are you thinking of? Unless you are doing this often the effort to automate may not be worth it (unless you also want to learn VS automation better :-)).
Richard
@Kyralessa: in this context, yes. Hitting alt+down arrow brings up the menu (with the options to insert the using directive or to expand the type with its full name); you press down arrow again to select the desired one and hit enter to select. It's down to personal preference, in the end, that's why I gave it as an example :)
Dan C.
+4  A: 

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.

tanascius
Or instead of using System, use something you know they'll have, like the namespace keyword.
Kyralessa
A: 

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.

ScottCate
ReSharper sure can do solution-wide cleanup. Right-click solution node in Solution Explorer and select Cleanup Code.
Ilya Ryzhenkov
+2  A: 
  • 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.
Ilya Ryzhenkov