2007-10-11

Changing Visual Studio Item Templates

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 I find myself repetatively doing the same thing in certain scenarios.

For example -- Making a new class.

Now, I usually just right-click on my project list, Add..., New Item, then I'm presented with an array of the available Item Templates. So, I'll select Class, rename it, and then I'm presented with this:


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

namespace MyNamespace
{
class Class1
{
}
}


And of course, the first thing I will do, is change the class name, set it to public, create an empty constructor, define code regions to keep things organized... 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();
}

///
/// Initializes field values.
///

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! That takes quite a few minutes of typing... FOR EVERY CLASS I WRITE!!

So, I decided to go figure out how those templates work.

Where are those 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 I want to fix, I am 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 contians 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 paramaters 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. There is a 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...