Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file on relative path using T4?

I'm trying to run a T4 template that opens a XML file and uses it contents to generate a code artifact. However, I'm getting the an error message when I try to run a T4 template similar to the one below

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Xml.dll" #>
<#@ assembly name="System.Xml.Linq.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ output extension=".cs" #>
namespace ConsoleApplication1
{
<# XElement fragment = XElement.Load("data.xml"); #>
...

Visual Studio 2010 error list is showing the following message

Running transformation: System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\data.xml'.

It is trying to open the file on the path where the TextTemplateFileGenerator custom tool runs. I'd like it to open the file relative to my project path, because other developers on my team use different folder structures. Does anyone know if it is something possible to accomplish?

like image 561
CARLOS LOTH Avatar asked May 28 '10 19:05

CARLOS LOTH


1 Answers

Change hostspecific option in template directive to "true" and call Host.ResolvePath("data.xml").

like image 147
Oleg Sych Avatar answered Nov 11 '22 11:11

Oleg Sych