How to: Create Multi-file Item Templates

Item templates may only specify one item, but sometimes the item is made up of multiple files. For example, Windows forms item templates require the following three files:

  • A .vb file that contains the code for the form.

  • A .designer.vb file that contains the designer information for the form.

  • A .resx file that contains the embedded resources for the form.

Multi-file item templates require the use of parameters to ensure the correct file extensions are used when the item is created in Visual Studio. If you create an item template using the Export Template wizard, these parameters are automatically generated, and no further editing is required. The following procedure explains how to use parameters to ensure the correct file extensions are created.

To manually create a multi-file item template

  1. Create the item template the same way you would a single file item template. For more information, see How to: Manually Create Item Templates.

  2. Add TargetFileName attributes to each of the ProjectItem elements. Set the value of the TargetFileName attributes to $fileinputname$.FileExtension, where FileExtension is the file extension of the file being included in the template. For example:

    <ProjectItem TargetFileName="$fileinputname$.vb">
        Form1.vb
    </ProjectItem>
    <ProjectItem TargetFileName="$fileinputname$.Designer.vb">
        Form1.Designer.vb
    </ProjectItem>
    <ProjectItem TargetFileName="$fileinputname$.resx">
        Form1.resx
    </ProjectItem>
    

    When an item derived from this template is added to a project, the file names will be based on the name the user entered in the Add New Item dialog box.

  3. Select the files included in your template, right-click, select Send To, and click Compressed (zipped) Folder. The files that you selected are compressed into a .zip file.

  4. Place the .zip file in the user item template location. By default, the directory is \My Documents\Visual Studio 2008\Templates\ItemTemplates\. For more information, see How to: Locate and Organize Project and Item Templates.

Example

The following example shows a Visual Studio Windows form template. When an item is created based on this template, the name of the three files created will match the name entered in the Add New Item dialog box.

<VSTemplate Version="2.0.0" Type="Item"
    xmlns="https://schemas.microsoft.com/developer/vstemplate/2005">
    <TemplateData>
        <Name>Multi-file Item Template</Name>
        <Icon>Icon.ico</Icon>
        <Description>An example of a multi-file item template</Description>
        <ProjectType>VisualBasic</ProjectType>
    </TemplateData>
    <TemplateContent>
        <ProjectItem TargetFileName="$fileinputname$.vb" SubType="Form">
            Form1.vb
        </ProjectItem>
        <ProjectItem TargetFileName="$fileinputname$.Designer.vb">
            Form1.Designer.vb
        </ProjectItem>
        <ProjectItem TargetFileName="$fileinputname$.resx">
            Form1.resx
        </ProjectItem>
    </TemplateContent>
</VSTemplate>

See Also

Tasks

How to: Create Item Templates

How to: Manually Create Item Templates

How to: Substitute Parameters in a Template

Concepts

Template Parameters

Other Resources

Creating Item Templates