38

After writing a whole bunch of code - i am finally waking up to adding CA and StyleCop to my solution.

By default all files (a lot of them) in my solution have using statements at the top of the file before the namespace.

I have resharper 6.0 and powertools in visual studio. Is there a way using these or any other tool that will go through all my CS file in the solution and put the using statements inside the namespace for each file.

MoXplod
  • 3,813
  • 6
  • 36
  • 46
  • 4
    Why do you want to have the usings inside the namespace? Having them out is the normal style and I don't see any advantage in having them in. – svick Nov 17 '11 at 01:01
  • 2
    it gives you style cop warnings if they are outside the namespace and the following link explains it better than i can - http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace – MoXplod Nov 17 '11 at 01:15
  • 6
    I was a crazy little boy to follow everything StyleCop tells me, but soon I discover that I should be tolerable and disable some of its warnings. Don't go extreme if you maintain a lot of existing code. Start from new code only. – Lex Li Nov 17 '11 at 03:30
  • But for people who's business it is to refactor code i.e. resharper etc. You would assume that they would have a simple way to just do this ( seems like a simple task - go to all .cs files ad move all using statements inside namespace ) .. hell i should just write a quick script for that :) – MoXplod Nov 17 '11 at 04:42

2 Answers2

51

With Resharper it can be done in two steps.

  1. Set the checkbox "Add 'using' directive to the deepest scope" in Code EditingC#Code StyleReference qualification. Options v9

  2. In the context menu of the solution choose "Cleanup Code...". SLN context menu

You might want to create a custom cleanup pofile that suits your needs before performing the cleanup.

In ReSharper before v9 the setting is located somewhere else: Options v8

Gebb
  • 6,371
  • 3
  • 44
  • 56
  • What is a custom cleanup profile? Is it easy to setup? So that i can just perform this and it doesnt do other things along with it? – MoXplod Nov 18 '11 at 00:20
  • @MoXplod A custom profile is a set of your preferences of what ReSharper should do when cleaning up your code. Though the level of customization is not really high, so I'm not sure if it's possible to configure it to only move usings and not do anything else. A profile can be created in Resharper - Options - Code Cleanup. – Gebb Nov 18 '11 at 03:43
  • This cleanup profile management is not the best thing in ReSharper, but it worked nice for me! – remio Jan 24 '12 at 12:56
  • I found that un-checking the box didn't work as it just removed the entry from the DotSettings file. I checked the box and then manually edited the DotSettings file changing the value for AddImportsToDeepestScope from True to False. – Liazy May 12 '16 at 11:28
5

In ReSharper 10 you can find it in: enter image description here

Diomedes Domínguez
  • 1,093
  • 12
  • 22
  • In Resharper 2020.2: Code Editing -> C# -> Syntax Style -> Reference Qualification and 'using' Directives -> Add 'using directive to deepest scope. Not sure why they have to move all the settings with every new version! – Dan Bailiff Sep 21 '20 at 20:52