Changing Visual Studio Item Templates

Written by Troy Howard

12 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:

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

namespace MyNamespace
{
   class Class1
   {
   }
}

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:

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

namespace MyNamespace {

    public class Class1 {

        #region Constructors

        public Class1() {
            Init(); 
        }

        /// <summary>
        /// Initializes field values.
        /// </summary>
        private void Init() {

        }

        #endregion Constructors

        #region Fields
        #endregion Fields

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Private Methods
        #endregion Private Methods
    }
}

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:

<ProjectItem ReplaceParameters="true">
Class.cs
</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:

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

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

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:

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

namespace $rootnamespace$ {

    public class $safeitemrootname$ {

        #region Constructors

        public $safeitemrootname$() {
            Init(); 
        }

        /// <summary>
        /// Initializes field values.
        /// </summary>
        private void Init() { }

        #endregion Constructors

        #region Fields
        #endregion Fields

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Private Methods
        #endregion Private Methods
    }
}

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…