Freigeben über


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 to organize your code. Also, you can reuse your code in different text templates. For more information, see Directive Syntax (Domain-Specific Languages).

To include a file in a text template

  1. In Solution Explorer, right-click the text template file that you want to edit, and then click Open.

    The template opens in the Text Editor.

  2. 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 look like the following:.

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

    Hinweis

    To debug text templates, you must set the debug parameter of the template directive. For more information, see How to: Debug Text Templates.

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:

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

Warnung

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

To add an include directory to the registry

  1. On the Start menu, click Run.

  2. In the Run dialog box, type regedit, and then click OK.

    The Registry Editor appears.

  3. Browse to the location HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\TextTemplating\IncludeFolders, and click the node.

  4. On the Edit menu, point to New, and then click Key.

  5. Type the file extension of your domain-specific language as the name of the new key.

    Hinweis

    The file extension is the one that you specified in the Domain-Specific Language Designer Wizard.

  6. Right-click the new key, click New, and then click String Value.

  7. Type Include0 for the name of the new string.

    Hinweis

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

  8. Right-click Include0, and click Modify.

  9. In the Edit String dialog box, in Value data, type the path of the folder that you want to check for included files. For example, type C:\TextTemplateUtilities.

    When you finish, your new registry key should look like this:

    Name

    Type

    Data

    (Default)

    REG_SZ

    (value not set)

    Include0

    REG_SZ

    <Your Path>

Security

For more information, see Security of Text Templates.

See Also

Concepts

Using Built-in Directives in Text Templates

Adding Code to Text Templates

Generating Artifacts Using Text Templates

Walkthrough: Creating and Running Text Templates