Click here to Skip to main content
6,597,576 members and growing! (22,864 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

Templates in Visual Studio .NET

By Ricardo Casquete

This article shows the way to create templates for Visual Studio .NET
Windows, .NET, Visual Studio, Dev
Posted:27 Mar 2006
Views:26,780
Bookmarked:35 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.57 Rating: 3.57 out of 5
1 vote, 10.0%
1
1 vote, 10.0%
2
3 votes, 30.0%
3
2 votes, 20.0%
4
3 votes, 30.0%
5
Sample Image - AddBllClass.jpg

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 for public/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:

Sample screenshot

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

Wizard Folder

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.

Icon of the Template

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

License

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

About the Author

Ricardo Casquete


Member
Ricardo Casquete is a Spaniard Computer Engineer who is worknig as Microsoft Technologies Architect.


Currently he is working in London and enjoy working and living among the Brits.

He is an MCAD.NET and MSCD.NET and he is enjoys working with Microsoft products like Sharepoint, the Enterprise Libary, Reporting Services, Commerce Server ...
Occupation: Web Developer
Location: Australia Australia

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralVS2005 code snippets Pinmemberken_in_oz16:32 4 Apr '06  
General"Very little" Info? PinmemberM.Lansdaal10:23 4 Apr '06  
GeneralRe: "Very little" Info? PinmemberRicardo Casquete23:38 4 Apr '06  
QuestionFunny and Easy? PinmemberOskar Austegard5:32 4 Apr '06  
AnswerRe: Funny and Easy? PinmemberRicardo Casquete6:31 4 Apr '06  
GeneralMore PinmemberErlend Robaye23:01 27 Mar '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Mar 2006
Editor: Deeksha Shenoy
Copyright 2006 by Ricardo Casquete
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project