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?
Design-time T4 text templates let you generate program code and other files in your Visual Studio project.
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.
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.
I found the answer:
Path.GetDirectoryName(Host.TemplateFile)
Another (perhaps a little quicker) way to do it is Host.ResolvePath(localFilePath).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With