visual-studio 使用 T4 获取项目或相关目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1352570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Project or Relative Directory with T4
提问by Blake Taylor
How can I get a reference to the directory of the visual studio project or solution or the directory of the t4 template from within a t4 template?
如何从 t4 模板中获取对 Visual Studio 项目或解决方案目录或 t4 模板目录的引用?
I have a template that concatenates a number of files together which are located relative to the template. I need to get a reference to there absolute location through a relative means. Hard coding an absolute path is not acceptable because the visual studio project will not always be in the same location and this would break the code generation. Reading the current working directory from the environment doesn't work either because that returns the location of where the T4 engine lives not the template. I'm just getting in to t4 so any help would be appreciated.
我有一个模板,它将多个文件连接在一起,这些文件相对于模板定位。我需要通过相对方式获得对绝对位置的引用。硬编码绝对路径是不可接受的,因为 Visual Studio 项目并不总是位于同一位置,这会破坏代码生成。从环境中读取当前工作目录也不起作用,因为它返回的是 T4 引擎所在的位置而不是模板。我刚刚进入 t4,所以任何帮助将不胜感激。
回答by Blake Taylor
回答by johnny 5
You can grab the path like this aswell
你也可以这样抓取路径
<#@ template hostspecific="true" #>
<#= Path.GetDirectoryName(this.Host.TemplateFile) #>

