ITextTemplatingEngineHost.ResolvePath 方法

允许主机提供完整路径以及给定文件名或相对路径。

命名空间:  Microsoft.VisualStudio.TextTemplating
程序集:  Microsoft.VisualStudio.TextTemplating.Interfaces.10.0(在 Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll 中)

语法

声明
Function ResolvePath ( _
    path As String _
) As String
string ResolvePath(
    string path
)
String^ ResolvePath(
    String^ path
)
abstract ResolvePath : 
        path:string -> string 
function ResolvePath(
    path : String
) : String

参数

返回值

类型:System.String
包含绝对路径的 String

备注

如果文件名不包含路径,则指令处理器可以调用此方法。 主机可以尝试通过搜索文件的特定路径并返回找到的路径来提供路径信息

对于每个文本模板转换,可以调用此方法 0 次、1 次或多次。 有关更多信息,请参见 T4 文本模板指令

主机可以在不同位置按它想要的顺序搜索程序集,或者添加程序集引用开始的选择路径。

示例

可从文本模板调用此方法。 必须设置 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 #>

下面的代码示例演示了自定义主机的可能实现。 此代码示例摘自一个更大的示例。 有关完整的示例,请参见演练:创建自定义文本模板宿主

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;
}

.NET Framework 安全性

请参见

参考

ITextTemplatingEngineHost 接口

Microsoft.VisualStudio.TextTemplating 命名空间