Click here to Skip to main content
11,705,610 members (51,202 online)
Click here to Skip to main content

Tagged as

Adding your items to Visual Studio default files

, 9 Oct 2011 CPOL 12.1K 14
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\

For Visual Studio 2013, the folders are just 12.0 versions:

C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\

In either case, the Class template is stored under:

\ItemTemplates\CSharp\Code\1033\

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

For VS 2010

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.

For VS 2013

The templates are no longer stored in ZIP files, but as "plain source" files.

For all versions

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
        #endregion
 
        #region Fields
        #region Internal
        #endregion
 
        #region Property bases
        #endregion
        #endregion
 
        #region Properties
        #endregion
 
        #region Regular Expressions
        #endregion
 
        #region Enums
        #endregion
 
        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public $safeitemrootname$()
            {
            /// TODO: Default constructor
            }
        #endregion
 
        #region Events
        #region Event Constructors
        #endregion
 
        #region Event Handlers
        #endregion
        #endregion
 
        #region Public Methods
        #endregion
 
        #region Overrides
        #endregion
 
        #region Private Methods
        #endregion	
        }
    }

Save the file, and update "class.zip" with the new version (If applicable)

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\"

or the appropriate equivilents for your VS version.

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:

CustomControl
UserControl
Form


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

IN EACH CASE, DO NOT FORGET TO COPY THE ORIGINAL FILES!
 

History

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

2015-04-26 Updated to reflect Visual Studio 2013 changes.

License

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

Share

About the Author

OriginalGriff
CEO
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?

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 4 Very nice! Pin
zenwalker19852-Oct-11 17:47
memberzenwalker19852-Oct-11 17:47 

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
Web03 | 2.8.150819.1 | Last Updated 9 Oct 2011
Article Copyright 2010 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid