Introduction to Templates in Visual Studio 2008


In this article, we will look into templates of VS 2008. Most of the projects, we create will be having structure or contents similar to one another. We need to do some minimal additions to it as per our requirements for each one. For example, we are writing some new modules [projects] for an application, each project will have similar contents like web.config, folder structure etc. So, it's better to have project template created with settings/contents common to all the projects. By using templates, we can save our time in doing repetitive tasks for each project. Templates automates those tasks, we use to do for each project. These templates are similar to those present in MS-OFFICE package. Visual Studio allows us to convert any existing project into a template easily without any additional effort.

Visual Studio provides two kinds of templates:

  • Project Template: This template will allows us to create a new project based on the exported project. This template will be accessible from New Project dialog window.
  • Item Template: This template will allows us to add our exported item as a part of their project. This can be accessed from Add New Item dialog window.

Now, let's look into a sample. Open your VS 2008 and create a new Windows Application with name as ProjectTemplateSample. Let's assume, most of my sample/test applications use to have below structure and contents:



I placed few controls on my MainForm. Every time, I create a new project, it will have same structure and contents. So, I will convert it to a Project Template in the following way:

  • Go to File Menu and select Export Template.
  • Now Select Template Type as Project Template and click on Next.
  • Give Template Name as SampleTemplate and Description as Sample Project Template.
  • Check "Automatically import templates" into Visual Studio.
  • Check "Display explorer window on output files folder" and click Finish.

Now, it will show the path of the template stored. This will be zipped and can be used to import into other machine's Visual Studio. We can import a template by just copying the zip into Visual Studio ProjectTemplates folder.

Next time onwards, we can create a project by using this template by just selecting it in New Project dialog window as shown below:



Now, we look into Item Template. We will export the above project's one of the item like ChildForm.cs using Item Template in this way:

Go to File -> Export Template -> Template Type as Item Template -> Click on Next -> Check ChildForm.cs and Select References -> Name as SampleItemTemplate and Description as Sample Item Template -> Finish.

This item can be accessed from Add New Item Dialog window as shown below:




We can import these templates by just copying it into your ItemTemplates folder of Visual Studio.

I am ending the things here. I hope this article will be helpful for all.

Up Next
    Ebook Download
    View all
    Learn
    View all