Get Project or Relative Directory with T4

主宰稳场 提交于 2019-12-03 02:34:48

问题


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?

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.


回答1:


See The Host Specific Parameter section HERE.

This snippet shows how to get the full path of src relative to the template.

<#@ template hostspecific="true" #>
// The location of the src folder relative to the t4 template:
// <#= Host.ResolvePath("src") #>



回答2:


You can grab the path like this aswell

<#@ template hostspecific="true" #>
<#= Path.GetDirectoryName(this.Host.TemplateFile) #>


来源:https://stackoverflow.com/questions/1352570/get-project-or-relative-directory-with-t4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!