Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ascertaining Project Directory from T4 Templates

Tags:

t4

I am using T4 templates in Visual Studio for code generation, and am trying to reference a local .xml file from the template code so I can parse it for metadata about the application's data model.

When the template code (which runs at compile time or when you run "Execute Custom Tool" on the template) tries to obtain the current path through Environment.CurrentDirectory, I get this:

c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE

I'm really trying to find the path of the template itself, within the project, so that I can use relative pathing to find the .xml file that's driving it. Any ideas?

like image 635
blaster Avatar asked Jan 18 '10 14:01

blaster


People also ask

What is the purpose of using T4 templates?

Design-time T4 text templates let you generate program code and other files in your Visual Studio project.

How to debug T4 templates?

To debug a design-time text template, save the text template file, and then choose Debug T4 Template on the shortcut menu of the file in Solution Explorer. To debug a run-time text template, simply debug the application to which it belongs.

What are T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.


2 Answers

I found the answer:

Path.GetDirectoryName(Host.TemplateFile)

like image 140
blaster Avatar answered Sep 19 '22 13:09

blaster


Another (perhaps a little quicker) way to do it is Host.ResolvePath(localFilePath).

like image 20
Oleg Sych Avatar answered Sep 16 '22 13:09

Oleg Sych