Changing the default class template in Visual Studio 2010

How many times have you hit SHIFT+CTRL+A to get a new class in a Visual Studio 2010 project? 100s? 1000s?

And after, that how many times do you have to modify the class file it creates because it is not exactly what you wanted… For me: Every single time!

For starters I want the class public by default. I also want standard regions installed to save me adding them afterwards.

Here’s how to do it:

1. Find the files

The files have not moved around much so are currently in C:\<Program Files>\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

The file you want for a class is strangely enough called If you unzip that file you will find it contains only 2 files:

  • Class.cs
  • Class.vstemplate

2. Extract the files

Open that zip file and place the 2 files somewhere to edit. You will need to zip them up again afterwards.

3. Backup the original Zip file!

Very important step as things do go wrong. Backup the original file e.g. by renaming it to Class.ZipOrig so that it is not recognised by Visual Studio.

4. Change the Class.cs template file

This is where you get to be creative. You will notice the original looks something like this:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
	class $safeitemrootname$

You might want something more like this:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
	public class $safeitemrootname$
		#region Constants
		#endregion Constants

		#region Fields
		#endregion Fields

		#region Properties
		#endregion Properties

		#region Constructors
		#endregion Constructors

		#region Public methods
		#endregion Public methods

		#region Class override methods
		#endregion Class override methods

		#region Class extensions - virtual methods
		#endregion Class extensions - virtual methods

		#region Private helper methods
		#endregion Private helper methods

You will notice a number of Macros $ commands in the template. The complete list is here.

4. Create a new file
After editing and saving the template, select the 2 files and use the “Add to compressed folder” option to save them back to a file called

5. Tell Visual Studio about the changes
Your new changes will not be loaded unless you explicitly tell Visual Studio to reload all templates.

  • Close Visual Studio (or the change swill not show until next time you run it)
  • Open a command prompt (you should run this as Administrator if you are not an admin of the machine).
  • Change to the IDE folder a few levels above the template folder (e.g. to C:\<Program Files>\Microsoft Visual Studio 10.0\Common7\IDE)
  • Run the following command:
    devenv.exe /installvstemplates
Viola! There you have your own custom template for new classes.

6. Automate repetitive/fiddly  tasks

If you are going to do this a lot I would suggest creating a batch file, e.g. in your template folder, to run the update step for you. For example create a text file called UpdateTemplates.bat containing the following 3 lines:

cd ../../../..
devenv.exe /installvstemplates

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

WordPress Themes