Click here to Skip to main content
11,409,097 members (40,553 online)
Click here to Skip to main content

Tagged as

Adding your items to Visual Studio default files

, 9 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Adding your items to Visual Studio default files
My code includes Regions: A Fields region, Properties region, Constructor region, and so forth, which I have to create manually when I create a new Form, UserControl, or project. Or, at least, I did, until I remembered how to change the existing VS templates.

I am assuming here that you have WinZip installed - if you don't, either get a free demo, or handle ZIP extract / insert yourself.

VS Templates are stored as text files with ZIPs in one of two directory structures, depending on your OS (assuming you installed VS in the default location)

For 32 bit:
   C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\
For 64 bit:
   C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\
In either case, the Class template is stored under:
In a file called "Class.Zip".
Important edit for Vista / Windows 7:
Before you go any further, you need to apply security attributes to allow you to modify the content of the zip files.
Right click the ItemTemplates folder, and select "Properties". In the resulting dialog, select the "Security" tab, and use the "Edit..." button to change priorities so that you can write and modify folder contents. If you do not, you will get "disk full" messages from WinZip when you try to save the files back.
End Vista / Windows 7 edit

Firstly: Open an explorer window in the template directory, and create a new folder called "Original". Copy all the zip files to this folder. That way, you can restore them to the original if you completely mess things up...

Double click the "Class.Zip" file, and open "Class.cs" for editing.
Replace the content with either your prefered items, or this:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
    /// <summary>
    /// TODO: Class description
    /// </summary>
	public class $safeitemrootname$
        #region Constants
        #region Fields
        #region Internal
        #region Property bases
        #region Properties
        #region Regular Expressions
        #region Enums
        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public $safeitemrootname$()
            /// TODO: Default constructor
        #region Events
        #region Event Constructors
        #region Event Handlers
        #region Public Methods
        #region Overrides
        #region Private Methods
Save the file, and update "" with the new version.

Ensure VS is not running (this may not be essential, but it can't hurt!)

Now open a new command prompt as Adminstrator.
Important edit for Vista / Windows 7:
If you do not use Administrator mode when you run this, you will get a "The operation could not be completed" dialog.
The easiest way to do this is to right click on the "Visual Studio Command Prompt" in the Visual Studio Tools menu of the "Start button...Programs" list and select "Run as Administrator"
End Vista / Windows 7 edit

Change to the appropriate directory:
64 bit
   cd "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\"
32 bit
   cd "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\"
Run this:
   devenv.exe /installvstemplates
Do note that this runs in the background, and may not be quick (depending on your computer).

Now each time you add a new class, it will contain your items.

You will probably want to repeat this for the
\ItemTemplates\CSharp\Windows Forms\1033\ files:

And also the files under ProjectTemplates rather than ItemTemplates in order to include them with a new job!


[edit]Updated to include security permissions and administrator access requirements for Vista / Windows 7 - OriginalGriff[/edit]


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?
Follow on   Google+

Comments and Discussions

GeneralReason for my vote of 5 Very nice indeed! PinmemberNiklas Lindquist19-Jul-10 0:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 9 Oct 2011
Article Copyright 2010 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid