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$
{
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
public $safeitemrootname$()
{
}
#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.