次の方法で共有


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.

Comments

  • Anonymous
    April 10, 2007
    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.

  • Anonymous
    April 10, 2007
    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 ;-)

  • Anonymous
    January 18, 2010
    Just an update for anybody looking. In Visual Studio 2008 it's under %Program Files%Microsoft Visual Studio 9.0Common7IDEItemTemplatesCSharpCode1033Class.zip (Note the CSharpCode1033 directory difference).

  • Anonymous
    September 23, 2010
    Instead of modifying the template, can we do otherway? because this modified files need to be placed in every machine of each team member. if there is any way to setting any project properties will give more adventages in group activity for this any updated on VS2010?

  • Anonymous
    September 28, 2010
    @Madhu Babu, Try experimenting with the "Export Template..." option from the File menu in VS2010. Looks promising.

  • Anonymous
    March 31, 2012
    Awesome!  Thank you.  Why Diagnostics isn't in there to begin with is beyond me.  In release it gets compiled out, I would think...

  • Anonymous
    August 16, 2012
    The comment has been removed