Export (0) Print
Expand All

ITextTemplatingEngineHost.ResolvePath Method

Allows a host to provide a complete path, given a file name or relative path.

Namespace:  Microsoft.VisualStudio.TextTemplating
Assembly:  Microsoft.VisualStudio.TextTemplating.Interfaces.10.0 (in Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll)

'Declaration
Function ResolvePath ( _
	path As String _
) As String

Parameters

path
Type: String

The path to complete.

Return Value

Type: String
A String that contains an absolute path.

A directive processor can call this method if a file name does not have a path. The host can attempt to provide path information by searching specific paths for the file and returning the file and path if found

This method can be called 0, 1, or multiple times, for each text template transformation. For more information, see T4 Text Template Directives.

A host can search for the assembly in different locations, in the order it prefers, or add a path of its choosing to the start of the assembly reference.

You can call this method from a text template. You must set hostspecific="true".

<#@ template hostspecific="true" language="C#" #>
<#@ output extension=".txt" #>
<#@ import namespace="System.IO" #>
<#
 // Find a path within the same project as the text template:
 string myFile = File.ReadAllText(this.Host.ResolvePath("MyFile.txt"));
#>
Content of myFile is:
<#= myFile #>

The following code example shows a possible implementation for a custom host. This code example is part of a larger example. For the complete example, see Walkthrough: Creating a Custom Text Template Host.

public string ResolvePath(string fileName)
{
  if (fileName == null)
  {
    throw new ArgumentNullException("the file name cannot be null");
  }
  //If the argument is the fully qualified path of an existing file,
  //then we are done
  if (File.Exists(fileName))
  {
    return fileName;
  }
  //Maybe the file is in the same folder as the text template that 
  //called the directive.
  string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), fileName);
  if (File.Exists(candidate))
  {
    return candidate;
  }
  //Look more places.
  //More code can go here...
  //If we cannot do better, return the original file name.
  return fileName;
}

Show:
© 2014 Microsoft