February 25, 2021
Hot Topics:

Technique Sharing with Project Templates

  • By Paul Kimmel
  • Send Email »
  • More Articles »

Visual Studio .NET is a multifaceted gem. You can spend weeks, months, or perhaps a year or more exploring Visual Studio and the .NET Framework. For example, you might be exploring Visual Studio .NET and the Diagnostics namespace for several days before you stumble on BooleanSwitches. (I will get to what a BooleanSwitch is in a few minutes.) However after you have discovered BooleanSwitches, you will likely want to share this information.

A convenient way to cross-pollinate learning with advanced subjects is to create project and item templates. When you select File|New|Project or File|New|File in Visual Studio .NET, the dialog you see that allows you to pick items are defined by templates. For example, the ASP.NET Web Service applet in the New Project dialog makes it easy for you to start building Web Services; this item is defined by a template. What I am suggesting is that when you discover new things about Visual Studio .NET, go ahead and create templates. By creating and sharing these templates you will find it easier for every member of your team to take advantage of every aspect of Visual Studio .NET.

The template I will demonstrate is a BooleanSwitch configuration file. A BooleanSwitch is a class in the System.Diagnostics namespace. The BooleanSwitch supports toggling debugging and tracing code on and off without recompiling your application. The BooleanSwitch works by reading an XML value in the application configuration file. The idea is that if the switch is enabled then your debug and trace code will run; disable the switch and the debug and trace code doesn't run. (You probably have implemented something similar to this. Fortunately, now the .NET Framework supports managing debugging and tracing from an external source file, rather than every programmer implementing a custom version.)

Creating the Basic Project Template Structure

The project and item templates that already exist can be found in the default installation directory C:\Program Files\Microsoft Visual Studio .NET\Vb7\VBWizards. The basic mechanism is that some pre-defined template files exist in this directory. When you select an item in Visual Studio .NET a copy of these files is made and added to your project. For example, when you add a new class to your project (see figure 1) a templated file named file1.vb is uniquely renamed, a default class name is added to the template, and the file containing the class is added to your project. The easiest way for us to duplicate this kind of behavior is to, well, duplicate it.

Click here for larger image

Figure 1: The Add New Item dialog uses a templated source file to add a class to your project.

Every template has the same basic folder structure. Depending on the needs of each template you will find a Scripts, Templates, Images, and HTML folders. Each of these folders contains a sub-folder that indicates the language version of the template. For example, if you have installed the English version of Visual Studio .NET then you will have a 1033 sub-folder in each of these template folders. (Refer to figure 2 for a picture of the folder structure for the BooleanSwitch item template we are creating.)

Figure 2: The BooleanSwitch item template folder structure.

You could create the template from scratch, but it is much easier to find an existing template and copy that. There is an AppConfiguration template that adds an empty App.config file to your project. The state of a BooleanSwitch is defined in an application configuration file, so it makes sense to reuse AppConfiguration template as a starting point for our new template.

To get started copy the AppConfiguration template project from C:\Program Files\Microsoft Visual Studio .NET\Vb7\VBWizards\AppConfiguration (if you installed Visual Studio .NET in the default folder). Paste the AppConfiguration file to the same folder and rename it to BooleanSwitch. The remaining steps will be to modify the template files, create a wizard launching file, a VSDir file, and test the project template. I will explain each of these pieces in the rest of this article.

Page 1 of 2

This article was originally published on September 13, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date