Udostępnij za pośrednictwem


How to: Manually Create Project Templates

Visual Studio provides wizards to help you generate templates from projects that are open in the integrated development environment (IDE). However, if you want to create a template outside the IDE, you can use the following steps to assemble the source files of your application into a Visual Studio project template.

The process of creating a custom project template involves designing and developing the project, and providing metadata about the project template.

Custom project templates require the following items:

  • A metadata (.vstemplate) file that Visual Studio uses to define how to display the project in the IDE and, if you have specified the appropriate properties, to customize how the project is created in the IDE.

  • A project file.

  • Code files such as Windows forms, Web forms, class files, and resource files.

All the items are compressed into one .zip file. The .zip file must be placed in the location recognized by Visual Studio for custom project templates. Custom project templates are listed in every programming language folder in the My Templates section of the New Project dialog box. For more information, see How to: Locate and Organize Project and Item Templates.

Note

You create Visual C++ templates differently from other language templates. For more information, see Designing a Wizard and Inside Visual C++ Wizards.

To create a template manually

  1. Create a project.

  2. Edit the code files to indicate where parameter replacement should take place. For more information about parameter replacement, see How to: Substitute Parameters in a Template.

  3. Create an XML file and save it as MyTemplate.vstemplate file in the same directory as your project, but do not add it to the project in Visual Studio.

  4. Author MyTemplate.vstemplate file to provide project template metadata. For more information, see the example in the following section.

  5. Select the files in your template (including the .vstemplate file), right-click them, click Send To, and then click Compressed (zipped) Folder. The files that you previously extracted are compressed into a .zip file.

  6. Put the .zip template file in the Visual Studio project template directory. This directory is under the Visual Studio installation directory and is called, Templates\ProjectTemplates\.

Example

This example shows a basic .vstemplate metadata file. The TemplateData element specifies the project type, name, description, and icon that are displayed in the New Project dialog box. The TemplateContent element specifies the name of the project file and the files that are in the project. In this example, the project file is named MyTemplate.csproj, and includes the files that a Visual C# project typically contains.

<VSTemplate Version="3.0.0" xmlns="https://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData>
    <Name>My template</Name>
    <Description>Basic C# application</Description>
    <ProjectType>CSharp</ProjectType>
    <ProjectSubType>
    </ProjectSubType>
    <SortOrder>1000</SortOrder>
    <CreateNewFolder>true</CreateNewFolder>
    <DefaultName>Simple</DefaultName>
    <ProvideDefaultName>true</ProvideDefaultName>
    <LocationField>Enabled</LocationField>
    <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
    <Icon>__TemplateIcon.ico</Icon>
  </TemplateData>
  <TemplateContent>
    <Project TargetFileName="MyTemplate.csproj" File="Killer.csproj" ReplaceParameters="true">
      <ProjectItem ReplaceParameters="true" TargetFileName="App.config">App.config</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="Form1.cs">Form1.cs</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="Form1.Designer.cs">Form1.Designer.cs</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="Program.cs">Program.cs</ProjectItem>
      <Folder Name="Properties" TargetFolderName="Properties">
        <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="Resources.resx">Resources.resx</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="Resources.Designer.cs">Resources.Designer.cs</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="Settings.settings">Settings.settings</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="Settings.Designer.cs">Settings.Designer.cs</ProjectItem>
      </Folder>
    </Project>
  </TemplateContent>
</VSTemplate>

See Also

Tasks

How to: Manually Create Web Templates

How to: Create Project Templates

How to: Substitute Parameters in a Template

Reference

Visual Studio Template Schema Reference

Other Resources

Starter Kits

Visual Studio Templates

Export Template Wizard