41

How do I prevent the name of the Solution Folder in a VS project from being appended to the namespace generated for new items added to the solution folder?

Example: C# project in a VS solution Default Namespace set in C# project properties: "BigClient.Domain"

If you create a solution folder in this project called "MySpecialStuff" and then add a new class to the 'MySpecialStuff" solution folder, VS creates the new .cs file with a "BigClient.Domain.MySpecialStuff" namespace. I want to find a way to allow the namespace of the newly-added class to retain just the 'project-level' namespace of "BigClient.Domain" instead of VS appending the "MySpecialStuff" solution-folder-name to the namespace.

I seem to recall reading a blog post by someone that this was possible (either via options/settings in VS or a registry setting) but I cannot recall where or how now that I want it :)

My platform is VS 2008 if answer = different for different VS versions.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
sbohlen
  • 1,999
  • 1
  • 15
  • 18
  • 6
    This has been a pet-peeve of mine *forever*. – Yoopergeek Aug 29 '09 at 18:31
  • 2
    Note: what is being referred to are not solution folders. They are folders in the project. A solution folder is a folder within the solution but not inside of any project. – John Saunders Nov 17 '10 at 20:50
  • Quite right -- good catch. Title updated to reflect this (important) distinction. – sbohlen Nov 18 '10 at 23:23
  • 3
    I know this has been answered, but if you select on the folder in the solution explorer, then properties, set Namespace Provider = False – MarkDav.is Nov 29 '10 at 23:18

2 Answers2

7

If you have ReSharper installed, on the settings for the directory (F4), set Namespace Provider to false.

bdukes
  • 152,002
  • 23
  • 148
  • 175
  • don't seem to see that anywhere -- you mean the properties for the solution folder itself in VS? Only property listed is 'Folder Name' (which, of course, is the name of the folder itself) – sbohlen Feb 02 '09 at 17:10
  • 2
    Ah, nevermind. I guess that's a ReSharper setting, not built into Visual Studio – bdukes Feb 02 '09 at 17:42
  • I've set that to folder but still getting the namespace on new file included the directory. – Zyo Sep 24 '21 at 14:16
1

Make a new class template or change Visual Studio's class template. See the MSDN documentation for how to this. In the template file, you quickly see the line that inserts the 'safe' namespace name.

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
  • 9
    I'm not sure how this is the accepted answer. It doesn't answer the OP's original question. http://msdn.microsoft.com/en-us/library/eehb4faa%28VS.80%29.aspx lists all of the built-in template parameters and there appears to only be on parameter, $rootnamespace$ that has anything to do with namespaces, and that $rootnamespace$ appears to always include the folder name that the file is being added into. This is what the OP was hoping to eliminate. – Yoopergeek Aug 29 '09 at 18:53
  • 2
    I know this has been answered, but if you select on the folder in the solution explorer, then properties, set Namespace Provider = False – MarkDav.is Nov 29 '10 at 23:18
  • @Yoopergeek: $safeitemrootname$ | The safe file name (without extension) entered into the Add New Item dialog. For Multi-File templates, this is the safe file name of the current template item being processed from the .vstemplate. – devios1 Apr 23 '11 at 18:54
  • 7
    @MarkDavis, that only works with Resharper installed. – Mike Perrenoud Nov 23 '13 at 15:23