How To: Edit Visual Studio Templates

Every time you create a new item in a Visual Studio project you get some default text. For some it is exactly what you want. For others you find yourself consistently making the same additions/deletions. Thankfully, changing the default content in the templates is easy to do.

Every time you create a new item in a Visual Studio project you get some default text. For some it is exactly what you want. For others you find yourself consistently making the same additions/deletions. Thankfully, changing the default content in the templates is easy to do.

Steps

  1. Locate the templates directory.
  2. Change the template you want to change.
  3. Rebuild the template cache.

Locate the templates directory.

If you installed Visual Studio in its default location, you will find all the files of interest in the following two places:

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

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

Under both of these directories will be separate directories for C#, VB.NET, J#, and Web. Since every language has different templates, they are separated in their own directories. Web templates are separated, and under the web folder there are folders for the individual languages. In these directories (and in the 1033 directory that all of them have) are the templates. All the files under the ItemTemplates folder are zip files. Under the ItemTemplatesCache directory are folders that correspond to the zip files in the ItemTemplates directory. For example, in the C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033 directory is a zip file named Class.zip. This is the template for the Class file option in the Visual Studio dialog Visual C# projects. Its twin in the cache is C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplatesCache\CSharp\1033\Class.zip, which is the files that are contained in the Class.zip file.

Find the template you want and edit it.

So next find the item template that you would like to edit. Let us say you want to edit the normal class template, the one that you get when you pick "Class" in the new item dialog in a class library. That one is found in ItemTemplates\CSharp\1033\Class.zip. Find that file, back it up somewhere (just in case something goes wrong), and extract the contents. You will probably see the following:


using System;
using System.Collections.Generic;
using System.Text;
namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

I will take you through the changes I make. First, since I take them out more than I keep them in, I remove the second two using statement. Second, I put "public " in front of the class declaration. After that, I am done.

Next, delete the Class.zip in the ItemTemplates\CSharp\1033\ directory. Put the files that are in the folder that was extracted earlier into a new zip file called Class.zip, and place that new zip file where the old zip file was. At that point the edits are done.

If you were to go into Visual Studio after that and create a new class file you would notice that the changes were not yet in effect. To make that happen, you have to rebuild the template cache.

Rebuild the template cache.

It appears that Visual Studio pulls from the ItemTemplatesCache folder for its template items, not from the ItemTemplates folder. To get those changes into the ItemTemplateCache you can manually make the changes or you can have Visual Studio rebuild the template cache. This is done by opening up the Visual Studio 2005 Command Prompt and executing "devenv /installvstemplates". This will recreate the cache.

What you do not want to do is to just make the change in the ItemTemplatesCache folder and not in the ItemTemplates folder. If there is any reason in the future that the cache needs to be regenerated, all of your changes would be lost. When the cache is recreated the ItemTemplatesCache folder is deleted, so any local modifications will be lost.

Also, I would recommend shutting down Visual Studio with the rebuilding is done. Visual Studio does not need to be restarted for the change to be noticed, but it did give me an error once (sorry, do not remember what it said) when I rebuilt with Visual Studio opened, but not after I closed the IDE and ran it again.

Other Resources

Missing an item template in Visual Studio 2005? Try this... by Eric Hammersley

Comments

Serghei (Monday, January 30, 2012 4:36 AM)

Thanks for useful information.

Cosmic_Spy (Thursday, May 2, 2013 4:13 PM)

Thanks very much~~~