如何在 Visual Studio 2008 中通过 T4 文本模板访问 csproj 文件?

How can I access the csproj file via a T4 text template in Visual Studio 2008?

如何在 Visual Studio 2008 中通过 T4 文本模板访问 csproj 文件?

我已经试过了,但是没有用:

using(StreamReader assemblyInfoReader = File.OpenText(Host.ResolvePath("ProjectName.csproj")))
{
 ...
}

我收到的错误消息是 "Unable to locate file"。

当我键入绝对路径时,我收到的消息是 "The length cannot be zero"。

当我尝试打开另一个文件时,例如 AssemblyInfo.cs,它工作正常。

顺便说一句,我正在尝试从 csproj 文件中访问 ApplicationVersion 标记。这与返回的内容不同:

Assembly.GetExecutingAssembly().GetName().Version.ToString();

尝试使用此方法获取 .csproj 路径...

string csprojPath = Host.TemplateFile.Replace(Path.GetFileName(Host.TemplateFile), "ProjectName.csproj");

'Host.Templatefile' 获取 'TemplateFileName.tt'

的路径

这是假设 .TT 文件与您的 .csproj 在同一文件夹中