How to: Include Files in Text Templates

You can use the include directive to include files in text templates. When you add the include directive to a text template, the system processes the included file as if it were included verbatim in the text template. The include directive helps you organize your code. Also, it lets you reuse your code in different text templates. For more information, see Directive Syntax (Domain-Specific Languages).

To include a file in a text template

  • Add an include directive to the template, and specify the name of the file that you want to include in the file parameter.

    For example, your code will resemble the following.

    <#@ include file="c:\test.txt" #>

Adding Include Directories to the Registry

When you use the include directive, typically you specify the full path and the name of the file to include. If you are including a large number of files in your text templates, you can add include directories to the registry. After you add include directories to the registry, you can specify the file name only, without the path, for the file parameter in the include directive.

The location for include directories in the registry is as follows.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\TextTemplating\IncludeFolders

Warning

Incorrectly editing the registry can severely damage your system. Before you change the registry, be sure to back up any valued data on the computer.

To add an include directory to the registry

  1. Open the Registry Editor (regedit).

  2. Browse to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\TextTemplating\IncludeFolders, and click the node.

  3. Add a key and name it by using the file name extension of your domain-specific language.

  4. .Add a string value named Include0 that has a value equal to the path of the folder that you want to check for included files, for example, C:\TextTemplateUtilities.

    Note

    You can have more than one include folder for each file name extension. To add multiple folders, add multiple string values to the key, and name them Include0, Include1, Include2, …

    When you finish, your new registry key should resemble the following.

    Name

    Type

    Data

    (Default)

    REG_SZ

    (value not set)

    Include0

    REG_SZ

    <Your Path>

See Also

Tasks

Walkthrough: Creating and Running Text Templates

Other Resources

Using Built-in Directives in Text Templates

Adding Code to Text Templates

Generating Artifacts By Using Text Templates

Change History

Date

History

Reason

July 2008

Rewrote and refactored project.

Content bug fix.