Templates in Visual Studio .NET






3.75/5 (9 votes)
This article shows the way to create templates for Visual Studio .NET

Introduction
Creating templates for Visual Studio is, in my opinion, funny, useful and easy.
This article shows the way to create your own templates for Visual Studio. I have found little information about this issue.
Some points I have found useful for templates are:
- Standardize your classes: You can write the
#region
you want forpublic
/protected
/private
methods/properties/vars/const/constructors… - Show examples of code inside of the classes
- Add common or custom using to the class, the inheritance
Templates of Visual Studio
All information about the templates of Visual Studio are stored in the folder: c:\[ProgramFiles]\ Microsoft Visual Studio .NET 2003\VC#.
The important part of the structure of this folder is this:
To add a template, you only have to add a folder and files in that folder with the same structure. That’s why I think the easiest way to do it, is using a Deploy Project.
The Folders
The folder we have to pay attention to, as I have said before, is c:\[ProgramFiles]\ Microsoft Visual Studio .NET 2003\VC#.
We must pay attention to the folders: CSharpProjectsItems and VC#Wizards.
- The CSharpProjectItems folder contains two folders.
- The LocalProjectsItems, that contains the vsdir files for the Items that can be added to a Windows Application or a class Library.
- The WebProjectsItems, that contains the vsdir files for the Items that can be added to a Web Application or a Web Service.
-
These folders also contain the vsz files that point to the Wizards (see below).
- The VC#Wizards folder contains all the wizards for the New Items. All the Wizards always have the same structure.
- The Script\1033 folder contains a JS script file executed for Visual Studio that is used to replace the name of the class, the name of the project, namespace, and can be used to iterate and create controls or to show a form with options before adding the Item… Usually I put the same you can find in CSharpAddClassWiz Wizard folder, but if you have a look at a different one, you'll see there are differences between the Web items ones, Windows Forms ones and a dataset for example.
- The Templates\1033 folder contains a file called Templates.inf that contains a line with the item name, for each item that is going to be added to the project.
Wizard Structure
Files
- *.vsdir - These files contains all the Items that can be added to the project in this folder. Each line of the file is a template that will appear in Visual Studio.
Example of the Data Inside and the fields that must be modified to create our own templates.
..\..\CSharpAddBLLWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|Business Logic Layer ( BLL )|11|Class for the Business Logic Layer, with Samples|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4801|0|EnterpriseBLL.cs
..\..\CSharpAddClassWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|Enterprise Class|10|Class with the Enterprise Format|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4801|0|EnterpriseClass.cs.
- The first field is the path to the vsz file of the Template.
- The third field is the Text under the Image in the Dialog (see the Image above).
- The fifth field is the Text under the Folder List (see Image above).
- The seventh field is the Name of the Icon shown, that is embedded in the csproj.dll file. If this field is missing or you write an Id that doesn't exist in the DLL, the default value is used (100).
- The last one is the Name of the class to create.
If you try to open other's vsdir files, like the DataSet, WebService… maybe you will find that instead of the Text for some of the fields there is a number… this number as in the icon case, is the ID of the
string
embedded in the csproj.dll, that contains the text to display. - *.vsz - This file has information about the wizard that is stored in the folder VC#Wizards.
Example of the data in the file:
VSWIZARD 7.0 Wizard=VsWizard.VsWizardEngine.7.1 Param="WIZARD_NAME = CSharpMyTemplateBLL" Param="WIZARD_UI = FALSE" Param="PROJECT_TYPE = CSPROJ"
You can see other's file and change what you want; the only important point here is to write the Name of the Wizard properly, it must be the same that a folder contained in the VC#Wizards folder. because when you select the template that points to the vsz, the name of the wizard will be the one that will be executed.
- default.js - A JS file with script code that Visual Studio executes with the class to add as parameter, to change things like the name of the class, the name of the namespace, to add controls, to show a form before adding the item to customize it...
- templates.inf - This file contains a list with the files that will be added to the Project.
Icons
As I have mentioned before, the vsdir is all the information to display the item.
Most of the Icons of the templates are stored in a DLL called csproj.dll that is located in the folder, C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\vcpackages.
If you open it with Visual Studio, it is easy to see the Icons or add a new one.
..\..\CSharpAddClassWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|Enterprise Class|10|Class with the Enterprise Format|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4801|0|EnterpriseClass.cs
In my example for the seventh field, I have written 4801
that is the ID (or Name) of the Icon I have added to csproj.dll.
The Sample’s Ugliest Point
To see the Icon over the template in Visual Studio AddNewItem Menu, as I have mentioned before, to the csproj.dll an Icon must be added with the ID of the one referenced in each line of the vsdir files.
Instead of creating a Custom Action to add the resource, I have added a Custom Action to copy my DLL Icon with the new Icon. This craps a bit, the right solution should be, instead of delete and copy a new DLL, to open it with an instance of System.Resources.ResourceWriter
and add the Icon.
Conclusion
As you can see in the article, adding a template for Visual Studio .NET 2003 is a simple task, I am sure you'll find it at least interesting apart from amusing.
Right now, I am trying to find out the way to create templates for Visual Studio .NET 2005. I hope it will be as easy as in 2003.
History
- 27th March, 2006: Initial post