Modifing The Default Visual Studio Template files


public class MyClassExample
{
    #region members

    private bool isValid = false;
    private string myString = string.Empty;
    private string anotherString = “Another string”;

    #endregion

    #region constructors

    public MyClassExample()
    {
    }

    #endregion

    #region methods

    private int multipyByFive(int _number)
    {
        return _number * 5;
    }

    #endregion

    #region event handlers

    // whatever…

    #endregion

    #region public

    // methods
    public override string ToString()
    {
        return “My ‘ToString’ method string”;
    }
    //properties
    public bool IsValid
    {
        get{return isValid;}
    }

    #endregion

}

I do a lot of C# programming and love to use the “region” c# Preprocessor Directive to segment my code into logical areas. One of the features I really love about using the “region” statement is that it allows you to collapse all the stuff inside it when you are working. This hides code that you aren’t working on and allows better concentration on the code that you are working on. At least that is how it is for me, anyway.

I recently installed a new version of Visual Studio. I wanted to change the base class template files to include my Region directives statements when it first creates a new default class.

I know the popular method of defining members is to define them just before the method that uses it, but being an old programmer that still remembers “spaghetti” code, this practice reminds of those old days. Using the region directives, I like to create a “members” section and put all my member definitions there. Same with “methods,” “constructors,” “event handlers,” “public” (public methods and properties) and such, as seen on the right.

I had done this on my previous versions of Visual Studio but, it’s been a long time since I messed with it. So I brought up my favorite search engine and went on a ride.

I found quite a few results for my query and a lot of them on Microsoft’s site. I don’t know if you are like me, but it seems that any search result found on Microsoft’s site either doesn’t work, or Microsoft sends you on wild goose chases with pages that that don’t have much info on them, or broken links.

Anyway, I did find some links to other sites with some good info. One such site had the location:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

I went into that folder and made all my changes. Restarted Visual Studio and… Wol-la! Nothing… No change! Same ole empty class.

After spending what seemed like days, (actually less than 30 minutes) searching around, I finally found the “motherload.” Yay! Something that works!!!

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip

 

I went in and changed several different files besides just the “class.cs” file to also include the “regions” I wanted.

This folder emulates the zip file structure without being zipped. I would suggest making your changes both places as the name suggests, it’s a “cache” so I assume there is some chance that Visual Studio will update it sometime with what is in the zip files and you would lose what you changed in the cache folder. So… be careful.