Changing Visual Studio Item Templates

Written by Troy Howard

11 October 2007

So, Visual Studio is pretty great right? It makes a lot of things really easy, really automated... saves a lot of typing, etc... However, there are still some areas where people find themselves repetatively doing the same thing in certain scenarios.

For example — Making a new class.

Now, usually one would just right-click on my project list, Add..., New Item, then you are presented with an array of the available Item Templates. So, then you would select Class, rename it and be presented with this result:

 1using System;
 2using System.Collections.Generic; 
 3using System.Text; 
 4
 5namespace MyNamespace
 6{
 7   class Class1
 8   {
 9   }
10} 

The first thing many will do, is perform a bunch of tweaks to make it formatted the way they like it. Perhaps change the class name, set it to public, create an empty constructor, define code regions to keep things organized, etc... and end up with something like this:

 1using System; 
 2using System.Collections.Generic; 
 3using System.Text; 
 4
 5namespace MyNamespace {
 6
 7    public class Class1 {
 8   
 9        #region Constructors
10
11        public Class1() {
12            Init(); 
13        }
14     
15        /// <summary>
16        /// Initializes field values.
17        /// </summary>
18        private void Init() {
19        
20        }
21
22        #endregion Constructors
23     
24        #region Fields
25        #endregion Fields
26     
27        #region Properties
28        #endregion Properties
29     
30        #region Public Methods
31        #endregion Public Methods
32        
33        #region Private Methods
34        #endregion Private Methods
35    }
36} 

WHOA! now I'm not personally a fan of regions and think the private Init() method used here is terrible... but I'm not here to judge that. To each thier own. However, if there are things like this that you do for every class... Well, that takes quite a bit of typing. FOR EVERY CLASS YOU WRITE!!

Visual Studio obviously has some template somewhere that controls it's default layout. Let's go figure out how those templates work.

Where are the files at?

The templates are stored in your Visual Studio installation directory. If you're like me, and running a fairly recent version of Visual Studio 2005, installed with default configuration, your install directory will probably be:

C:\Program Files\Microsoft Visual Studio 8\ 

and the templates are stored in:

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates 

Under that directory you'll find subdirectories for each classification of template (CSharp, JSharp, VisualBasic, etc... ) and in each of those subdirectories, you'll find a zip file for each template.

For what we want to fix, we are looking for this file:

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip 

Sidenote: Notice that the last subdirectory in the path is the language code 1033 (English) and if you've installed windows/vs with a different language, this will be different.. this is one reason you may not see your default templates when working across languages (ie, windows is in spanish, and visual studio is installed from the English language version, but is configured to be in Spanish; templates will be missing!!

In the Class.zip file, you will find two files; Class.cs and Class.vstemplate

Class.vstemplate

Class.vstemplate is just an XML file. This contains information about the template, such as what GUID to lookup for the icon to display in the wizard screen, what the sorting order is, what assemblies it references, etc.. For the most part, unless your doing something more complicated than what I want to do, you won't need to edit this. One tag to pay attention to is:

1<ProjectItem ReplaceParameters="true">
2Class.cs
3</ProjectItem> 

This tag says that VS should make a new project item based on Class.cs, and it should parse the file and replace the parameter tokens in it with the appropriate values... So let's look at Class.cs:

Class.cs

Class.cs is the templated code file. The default file looks like this:

 1using System; 
 2using System.Collections.Generic; 
 3using System.Text;
 4
 5namespace $rootnamespace$
 6{
 7    class $safeitemrootname$
 8    {
 9    }
10} 

So this is the normal new empty class, and the tokens $rootnamespace$ and $safeitemrootname$ are what will get replaced when VS parses the file and passes in the parameters. Well, I don't know anything about those parameters... So I'm not going to mess with them. However, I did go make a list of the parameters I found in the default templates ( I could not find a list of these parameters on the net anywhere...)

  • $rootnamespace$
  • $safeitemrootname$
  • $registeredorganization$
  • $year$
  • $guid1$
  • $ContentTags$
  • $MasterPage$
  • $fileinputname$
  • $classname$
  • $safeitemname$

So I modified Class.cs to be:

 1using System; 
 2using System.Collections.Generic; 
 3using System.Text; 
 4
 5namespace $rootnamespace$ {
 6
 7    public class $safeitemrootname$ {
 8
 9        #region Constructors
10        
11        public $safeitemrootname$() {
12            Init(); 
13        }
14        
15        /// <summary>
16        /// Initializes field values.
17        /// </summary>
18        private void Init() { }
19
20        #endregion Constructors
21        
22        #region Fields
23        #endregion Fields
24        
25        #region Properties
26        #endregion Properties
27        
28        #region Public Methods
29        #endregion Public Methods
30        
31        #region Private Methods
32        #endregion Private Methods
33    }
34} 

Yay! my fingers are saved... but what you say? all my base are NOT belong to us? True. Someone set us up the cache.

Refreshing the Visual Studio Template Cache

The template files are cached in a folder called TemplateItemCache in the same location as the template folder. So...

  1. Close all Visual Studio Windows
  2. Clear contents of TemplateCache folder
  3. Open a DOS prompt (normal one, or Visual Studio 2005 Command Prompt)
  4. Run devenv /installvstemplates (if that doesn't work, run devenv /setup)

Now, when you restart Visual Studio, your new templates will be installed... BUT! the fun doesn't stop here! The same templating structure for items also applies to projects! But that's next post...