Steve's blog

Changing the default using directives in Visual Studio

The other day I was working on a demonstrator application for the Object Builder framework, and adding a lot of new classes to my project.  Having to manually add using directives for OB in every class didn't seem terribly efficient, so I set about finding how you can change the default C# class template.  I discovered this nugget in Anson Horton's blog - if you open %Program Files%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip, you can modify the class.cs file within that's used to generate all new C# source files - it looks like this:

using System;
using System.Collections.Generic;
using System.Text;

namespace $rootnamespace$
{
	class $safeitemrootname$
	{
	}
}

You can then add or remove the using directives you want at the top of this file, and save it back to the archive.  Finally run %Program Files%\Microsoft Visual Studio 8\Common7\IDE\devenv.exe /setup to refresh Visual Studio's template cache.  Now all new C# files you create should match your modified template.

Published Tuesday, April 10, 2007 8:44 AM by sthorne

Comments

 

AtulGupta said:

This is fine. However with VS2005 intellisence this isn't much of manual work. When we add a new type whose namespace hasn't been already added with the using statement, we immediately get the smart tag type option to either insert the using statement or use the full name in the code itself.

April 11, 2007 1:12 AM
 

sthorne said:

AtulGupta - Indeed we do, which is fine when adding a few similar files or many files with different namespace requirements.  However if we have a large project with standard library requirements then setting a template can remove the need to repeat these steps on every new file - after all as developers we're always after getting the machine to do as much of the work for us as possible ;-)

April 11, 2007 4:09 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker