Changing the default class template in Visual Studio 2012/2013

I moved to Visual Studio 2013 when it came out and have not had to go back to the 2012 version for some time now. That is a sign of how good the new version is. It does however mean this article now needs an update (again).

To recap:

How many times have you hit SHIFT+CTRL+A to get a new class in a Visual Studio 2012/2013 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 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

(in VS2012 they are in C:\<Program Files>\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033)

The file you want is in an appropriately named folder (no more ZIP files!). If you open the Class folder you will find the following 2 files:

  • Class.cs
  • Class.vstemplate

2. Backup the original files!

Very important step as things do go wrong. Backup the original files e.g. by copying it to Class.cs. Orig so that it is not recognised by Visual Studio.

3. 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. Save your changes
Simply save your edited files (again, there is no longer a ZIP file to recompress with VS 2012).

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 "C:\Prograsm Files (x86)\Microsoft Visual Studio 12.0\Common\IDE\"
devenv.exe /installvstemplates

WordPress Themes