共用方式為


Visual Studio Templates – Add New Item to project

Using Visual Studio to write code requires creating a project (well, you can use Visual Studio without a project, but you won’t get very much past Notepad functionality – there’s not even Intellisense on files) . After creating / opening an existing project, we can add new files (classes, class diagrams, XML files etc) by selecting the Project menu, then Add New Item (or add Class / Windows Forms / Windows Component or User Control, if this is what you need). After you specify all the details (file name), a file is added with some default text in it. Assuming you have Visual Studio 2008 SP1 installed, the templates that generate this text are stored at %ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates and %ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplatesCache (for x64 machines, look in % ProgramFiles(x86)% ).

Let’s add a new C# class, Tester, to the class library project SimpleTests. By default, in Visual Studio 2008 it will contain the following text:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace SimpleTests

{

    class Tester

    {

    }

}

 

This text comes from the Class template. If we look in the ProjectTemplateCache folder, inside CSharp (the language we used to add the class) -> Code -> 1033 -> Class.zip, we find a class.cs looking like this:

 

using System;

using System.Collections.Generic;

$if$ ($targetframeworkversion$ == 3.5)using System.Linq;

$endif$using System.Text;

 

namespace $rootnamespace$

{

      class $safeitemrootname$

      {

      }

}

 

This explains the text we get. But how can we customize the default? I really would appreciate some changes:

- I don’t want all these usings added by default – I prefer to start without any using statement, and to add them while the code requires them. Of course, I could do it the other way around: add all the usings I think I may need; when I am done coding, remove the unused ones. Visual Studio lets you do that easily by right clicking on the page, selecting Organize Usings -> Remove Unused Usings (or Remove and Sort).

- Second, I prefer Usings inside the namespace declaration and I want to add a header to the file.

I can change the Class.cs file and replace the Class.zip in ProjectTemplatesCache folder:

 

//------------------------------------------------------------

// Copyright (c) Oana Platon

//------------------------------------------------------------

 

namespace $rootnamespace$

{

    using System;

   

    class $safeitemrootname$

    {

    }

}

 

Then when I reopen Visual Studio and Add a class, I get this file:

 

//------------------------------------------------------------

// Copyright (c) Oana Platon

//------------------------------------------------------------

 

namespace SimpleTests

{

    using System;

 

    class MyCustomizedTester

    {

    }

}

 

Cool, right?

This change will be lost the first time when devenv /installvstemplates command is called (this command replaces the ItemTemplatesCache folder with what finds in ItemTemplates folder). To make it permanent, make the change in ItemTemplates folder instead and run devenv /installvstemplates to propagate the change to ItemTemplatesCache.

Comments

  • Anonymous
    April 21, 2009
    Very helpful! Works for commonly used references, too, which can be added to the .csproj file, so that you don't have to choose "Add Reference" every time you create a new project.

  • Anonymous
    August 29, 2012
    Thanks for the article. It got the task done in a couple of minutes. The below mentioned link has entries for all supported template parameters. msdn.microsoft.com/.../eehb4faa(VS.80).aspx