Create and Use a Template in C#

How to create and Use a Template in C#

Template is use to create a  Skeleton for New Item(File) or project     
   1. Itemtemplate (For File)
   2. ProjectTemplate (For Project)

How to Create  ItemTemplate

1. It require Template class file (.cs)
2. vstemplate (VisualStudio Template), It is a XML file having the extension as .vstemplate

All vstemplate files must have the following format:

<VSTemplate Version=”1.1.0″>
               <TemplateData>
                               <Name>
                               <Description>
                               <Icon>
                               <ProjectType>
                                              <Languages>
                                                             <Language>
                                              </Languages>
                               </ProjectType>
                               <SortOrder>
                               <DefaultName>
               </TemplateData>
               <TemplateContent>
                               <References>
                                              <Reference>
                                                             <Assembly>
                                              </Reference>
                               <ProjectItem>
                                              <SourceFile>
                                              <ReplaceParameters>
                               </ProjectItem>
               </TemplateContent>
</VSTemplate>

Example
<VSTemplate Version=”2.0.0″ xmlns=”http://schemas.microsoft.com/developer/vstemplate/2005″&gt;            <TemplateData>
                        <Name>Sample Template</Name>
                        <Description> This is the Sample Template for Testing /Description>
                        <Icon>Sample.ico</Icon>—————If u want any Icon for Template
                        <ProjectType>CSharp</ProjectType>
                        <DefaultName>Default Sample Name</DefaultName>–Default name for the File
           </TemplateData> 
            <TemplateContent>
    <References>
                          <Reference>
                                <Assembly>System</Assembly>
                </Reference>
    </References>
    <ProjectItem SubType=”Code” TargetFileName=”$fileinputname$.cs” placeParameters=”true”>SIMPLE.cs</ProjectItem>
            </TemplateContent>
</VSTemplate> 

Save the above file as “SIMPLE.vstemplate”

SIMPLE.cs is  class file to act as Template using the skeleton of  .vstemplate

using System;
using System.Collections;
namespace $rootnamespace$ —————————–Detect the Namespace name
{
    public class $safeitemname$—————————-Detect the class
    {
                public $safeitemname$()
                {
                }
     }
}

1 .Save the both file (SIMPLE.cs and SIMPLE.vstemplate) in to the same directory, for example SIMPLE
2. Make the above directory as a zip file. Now, we could put it in to the ItemTemplates directory \My Documents\Visual Studio 2005\Templates\ItemTemplates Drop the zip file in that folder and then load Visual Studio. Create a new project and right-click to add a new file. There you’ll see our new template, select the Template and enjoy coding.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: