Adsense

Tuesday, September 18, 2007

Visual Studio - Item Template Editing (Class, WPF, Forms, ETC...)

I've been working in Visual Studio for a few years now, and have created hundreds of classes, user controls, windows, forms, pages... You get the picture. I'm also the type of developer that likes to keep my code organized and generally start off with a couple of standard code regions (Fields, Properties, Events, etc...) in my classes and code-behind files.

I always imagined that there would be a simple way to override the default templates for these files to make my job easier, and today I finally spent the time to track it down...

Visual Studio Item Templates are located in the following directories...

Item Template Files

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

Item Template Cache Files

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

Within each of these directories you'll have folders for CSharp, VisualBasic, etc. These folders hold all of the Item Templates for the named language. To edit the base implementation of the Class.cs file, you would browse to "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip" and load up the Class.cs file. This file would look something like this by default.

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

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
        public $safeitemrootname$()
        {
        }
    }
}

You might end up modifying this file like so to add a few regions by default...

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

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
        #region Fields & Properties
        #endregion

        #region Constructors
        public $safeitemrootname$()
        {
        }
        #endregion

        #region Private Methods
        #endregion

        #region Public Methods
        #endregion
    }
}

The next time you add a Class.cs file to one of your projects, it will come complete with the regions that you added to the template. You can also customize these templates further using other template parameters (i.e. '$clrversion$' for the Current version of the common language runtime (CLR).)

You can find a list of template parameters HERE....

No comments: