Referencing a File by Relative Path with T4

This was way harder to figure out than it should have been, so for the next person that needs to reference an input file in a T4 template by its relative path, here is what you need to do…

1. Include…

<#@ include file=”” #>

2. Add hostspecific=”True” to your template setting…

<#@ template language=”C#” hostspecific=”True” #>

3. Import EnvDTE…

<#@ import namespace=”EnvDTE” #>

4. Now TransformationContext.Host.ResolvePath(“SomeFile.sdf”) will return the absolute path of “SomeFile.sdf”.

3 thoughts on “Referencing a File by Relative Path with T4

  1. I’m storing some of the raw data in text form within the project, which are sql statements that need to be applied to both Sql Server and Oracle. The whole project is in source control. Thus, being able to identify the directory of the tt file is essential.

    I was not able to find this info anywhere else. Thanks very much.

Leave a Reply

Your email address will not be published. Required fields are marked *