Tuesday, November 27, 2007

Save time with Visual Studio 2005 project templates

While application development provides plenty of opportunities to be creative, there are numerous aspects of every project that require developers to perform the same tasks over and over again. Automating such tasks provides you with more time to concentrate on the more challenging aspects of your work. Visual Studio 2005 provides numerous ways to boost productivity; templates are just one example. This article describes creating and using project templates.

Templates

Visual Studio 2005 has two different kinds of templates. The first is a standard template. Standard templates are included with a Visual Studio 2005 installation. You can find these templates in the Visual Studio 2005 directory under the Program Files directory (assuming the default directory is used during installation). On my system, the following directory points to the standard templates:

C:Program FilesMicrosoft Visual Studio 8Common7IDEProjectTemplates

The second type of template is user or custom templates. A registry key (HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0) specifies where custom project templates are stored on a development machine. The default directory for these templates is contained in the templates subdirectory of the project’s directory of the Visual Studio 2005 installation. The default directory is:

C:Documents and SettingsMy DocumentsVisual Studio 2005Templates

To control where project templates are stored via Visual Studio 2005, you simply select Tools | Options from the drop-down menu to open the Options window. In this window, expand Project and Solutions and select General. It includes a field for designating where project templates are stored.

Template file structure

Templates are contained in compressed files. A good example is the directory for standard Windows C# applications:

C:Program FilesMicrosoft Visual Studio 8Common7IDEProjectTemplatesCSharpWindows1033

By default, the following template files are included in this directory:

  • ClassLibrary.zip: Project template for a Class Library project.
  • ConsoleApplication.zip: Project template for a Windows console application project.
  • CRCSharp.zip: Project template for a Crystal Reports Application project.
  • EmptyProject: An empty project.
  • WebControlLibrary.zip: Project template for a Web Control Library project.
  • WindowsApplication.zip: Project template for a Windows Application project.
  • WindowsControlLibrary.zip: Project template for a Windows Control Library project.
  • WindowsService.zip: Project template for a WindowsService.zip.

The compressed files are standard, so you can easily view the files’ contents with a project like WinZip or via Windows XP compressed folder.

The zip files contain all files necessary for creating a project. In addition, it contains an extra file with the extension vstemplate. This template file contains all information necessary to create a project and add appropriate files to the project.

The vstemplate file stores project template meta data as XML. In addition, it includes the details of how the template is displayed and what is included with it. As an example of a standard project template, the WindowsApplication.zip file contains the following files: assemblyinfo.cs, consoleapplication.csproj, csconsoleapplication.vstemplate, and program.cs.

Creating your own template

The simplest way to create your own template is by using another template as the starting point; that is, create a new Visual Studio 2005 project by selecting a template. Once the project has been created, you can make your changes like adding forms, classes, and so forth. Finally, save the project.

Once the project has been saved, you can select Export Template from the File drop-down menu within Visual Studio 2005. This opens the Export Template Wizard window, which guides you through the creation of the project template.

First, you select the template type (which is project template in this example). A drop-down list is available to specify the project to be used as the base for creating the template (it defaults to the currently open project that you just saved). Another drop-down selection is present for choosing the language (your choices depend upon what is installed) that the template will exist.

At this point, you select the next button and a final window appears. This window allows you to specify an icon for the project template (it appears in the Create A New Project window) along with assigning a template name. Also, a field is provided to type a description of the template. The output location of the new project template file (compressed file) is displayed, but it may not be edited.

Finding templates online

In addition to the previously described standard templates that are included with Visual Studio 2005, there are starter kits available from Microsoft as well. The starter kit concept promotes the consumption of sample code applications and projects through community sites. These samples may be downloaded from the community Web sites.

You may search online for templates or starter kits when creating a new project. The New Project dialog box provides an option called Search Online Templates. This option allows you to search for starter kits online by keyword. If a match is found, you can choose to download and install it into the appropriate directory so it may be used as a template. These starter kits provide an opportunity to learn via code that has been tested and developed by professionals. The MSDN and MyCodezone sites are examples of online template resources.

Conserve time and energy

The main selling point of Visual Studio 2005 templates is the ability to automate redundant steps, thus saving developers time. Many organizations include a base set of features in new Visual Studio 2005 projects, including standard graphics, splash page, login form, specific references and so forth. By including these elements in a project template, you can easily create new projects with the template and avoid having to manually do it each time.

No comments: