Productivity Hack: Create Your Own Visual Studio 2005 Item Templates

Download the sample code for this post. 

I originally intended to write about customizing existing Visual Studio 2005 item templates such as the Class file to take the form of whatever you'd like, yet I decided that was the easy stuff.  The real power is in creating your own item templates.  You can likely discern how to customize an existing template based on this sample (email me if you have questions).  There are a zillion reasons (literally!) why you may want to do this but a simple example that comes to mind is a custom exception.  Granted the best practice of exceptions is to throw existing exception types but that would simply defeat the purpose of my example!

There are two important files included in this process:

1. Your custom file which is the template for the item
2. The .vstemplate file which provides some meta data about your custom item template

To spice up your custom template check out the available parameters to include in your file.

Here's a custom exception template which implements the four recommended constructors.  I've called the file Exception.cs.  Don't worry about naming conflicts with the base System.Exception type as the template specifies use of your project's root namespace.  Additionally the user has the option to change the filename (seen here as $safeitemname$) or if the suggested name is kept a number is automatically appended to the filename such as Exception1.cs.

Exception.cs

Exception Template 

Exception.vstemplate

 

Once you've created these files, zip them up into a file called, wait for it... Exception.zip.  Drop that file in your "user item templates location" which you can find by going to Tools > Options > Projects and Solutions > General.  So you have some general idea of where that might be on my system it's in C:\Documents and Settings\isuttle\My Documents\Visual Studio 2005\Templates\ItemTemplates\Visual C#.

Now for the feeling of accomplishment.  Create or open a project and add a new item to it.  You should see your "Exception Template" as an option under "My Templates." 

New item  

If all goes well you'll have a file looking something like this:

PastBedtimeException.cs 

Download the sample code for this post.

kick it on DotNetKicks.com
Bookmark and Share

Related posts

Comments

October 23. 2007 11:04 AM

trackback

Trackback from DotNetKicks.com

Productivity Hack: Create Your Own Visual Studio 2005 Item Templates

DotNetKicks.com

October 24. 2007 01:20 PM

Jozef Sevcik

Very nice, thanks for sharing this !

Jozef Sevcik

October 25. 2007 02:21 AM

George Chatzimanolis

Very nice... You have been kicked... ;)

George Chatzimanolis

October 25. 2007 04:17 AM

Armen Ayvazyan

Great. I will immediately start using your template.

Armen Ayvazyan

October 25. 2007 03:12 PM

trackback

Trackback from .Net Adventures

Interesting articles (October 26)

.Net Adventures

October 28. 2007 02:22 PM

pingback

Pingback from rtipton.wordpress.com

Weekly Link Post 13 « Rhonda Tipton’s WebLog

rtipton.wordpress.com

October 30. 2007 06:09 AM

Alun Harford

If the file gets called Exception.cs, you have a bug in there.
Although the class ends up in the right namespace,

public Exception(string message, Exception inner) : base(message, inner) {}

will not compile. You need:

public Exception(string message, System.Exception inner) : base(message, inner) {}

Alun Harford

October 30. 2007 10:42 AM

Ian Suttle

@Alun - System.Exception is not required unless you specifically name the new item "Exception" (not recommended). The template is already "using System;"

Ian Suttle

December 17. 2007 10:01 AM

Aaron

Have you figured out how to do this with Visual Studio 2008?

Aaron

January 4. 2008 01:26 AM

Ian Suttle

@Aaron - The directory structure is just a bit different. If you want to change a current template check out C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp and if you'd like to add your own as we've done in this example check out C:\Documents and Settings\ISuttle\My Documents\Visual Studio 2008\Templates (changing ISuttle of course). These are default locations which you can alter/view in Tools > Options > Projects and Solutions.

Ian Suttle

January 9. 2008 04:42 PM

Brett

@Alun Harford -- You're right, this should be fixed. Ian, why did you just wave your hands and say that it's not recommended? It's an easy fix, just do it.

Brett

January 9. 2008 05:40 PM

Ian Suttle

@Brett - Sorry, not trying to blow anyone off. You're probably right though... although it's not recommended it is more accurate.

Ian Suttle

July 27. 2008 04:12 AM

pingback

Pingback from www19.a2hosting.com

Interesting articles (October 26) | devintelligence.com

www19.a2hosting.com

February 26. 2009 08:00 AM

short jokes

its a creative article.i teaches in a good way..thanks for sharing..
Regards..

short jokes

Comments are closed