Editing Visual Studio Templates


I have seen a couple of posts lately with people asking how to customize what VS spits out when you create a new item in a project, so I decided to post. This post applies to Visual Studio 2008 only. For 2005 and earlier, the templates are still in roughly the same place, but they are not zipped up (actually making it easier to customize them?).

Editing the files

First, we need to find the templates. There are a variety of templates, and it depends on what you want to edit. The general location is:

C:Program FilesMicrosoft Visual Studio 9.0Common7IDE

Or for 64 bit

C:Program Files (x86)Microsoft Visual Studio 9.0Common7IDE

There are numerous directories under this folder, but we are only interested in two for most of our work: ItemTemplates and ProjectTemplates. These folders contain (big surprise coming ;->) item templates and project templates.

Folder

Let’s look at a web project template first. The zip file is located at:

C:Program FilesMicrosoft Visual Studio 9.0Common7IDEProjectTemplates{language}Web1033

We will open up the WebApplicationProject.zip file. Inside we find the following (pictured below):

WebProject

Opening the default.cs file yields the following:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

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

$endif$using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

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

$endif$

namespace $safeprojectname$

{

      public partial class _Default : System.Web.UI.Page

      {

            protected void Page_Load(object sender, EventArgs e) 

            {

 

            }

      }

}

Now lets move up to a new class template for a web project. You find this at:

C:Program FilesMicrosoft Visual Studio 9.0Common7IDEItemTemplatesWebCSharp1033

The file is webform.zip and we will want to edit codebeside.cs. The only real difference here is highlighted below:

public partial class $codebesideclassname$ : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

}

Tags available

Here are a list of some of the tags you might want to use to customize your templates.

·         $clrversion$ – current version of the CLR

·         $guid#$ (where # is a number between 1 and 10) – GUID that replaces the GUID in the project file

·         $itemname$ – Name provided by user (note that the web team has stepped out of the box here

·         $machinename$ – machine created on

·         $projectname$ – Name given in the new project dialog

·         $registeredorganization$ – Registry key value for HKLMSoftwareMicrosoftWindows NTCurrentVersionRegisteredOrganization

·         $safeitemname$ – Name provided by user with all unsafe characters replaced

·         $safeprojectname$ – ditto, but for a project

·         $time$ – Current date time in the format DD/MM/YYYY HH:MM:SS

·         $userdomain$ – Current user domain

·         $username$ – Current user name

·         $year$ –Current year in the format YYYY

Please note that the $time$ variable is in the format DD/MM/YYYY HH:MM:SS and is not localized for culture. This is probably not a big deal, but it should be noted. Here are a few “undocumented” tokens:

·         $fileinputname$ – unsafe file name, sans extensions

·         $fileinputextension$ – extension of above

·         $runsilent$ – In template, no clue what this does (Boolean value)

·         $itemname$ – unsafe item name

·         $itemrootname$ – Seems to be the same as above?

·         $rootname$ – ditto

·         $safeitemrootname$ – Safe file name without extension

·         $rootnamespace$ – Namespace at level you are creating your class.

Some people have stated that $rootnamespace$ is the namespace for the root of the project, indicating there is a different tag when you are going deeper. I have not found this to be true, which indicates this is the namespace at the root of the folder you are creating in. Not as big a deal for web projects, but it does have implications for windows programs, where you can add a folder and have the folder name automagically appended to your namespace.

Note that you can also create your own custom templates and pass custom parameters to them:

http://msdn.microsoft.com/en-us/library/ms247063(VS.80).aspx

The launch page for templates is here:

http://msdn.microsoft.com/en-us/library/6db0hwky(VS.80).aspx

Hope this helps!

Peace and Grace,
Greg

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: