T4 templates using own project .config file

One of the first problem one faces, as soon as he decides to include T4 templates in a project, is how to access .config file of the project. Most of the times .config files are used to store information as connection strings, as well as, other application wide settings, critical to the application. When a T4 template is transformed, it is not run under the context of the project it belongs; the context it runs is the context of the utility that performs the text transformation. Most of the times the utility is TextTemplatingFileGenerator.exe.

The need to access information from app.config from T4 templates, drive me create a T4 template include file, that gives me access to app.config information the same way as ConfigurationManagerobject. You can find this file here:

    I also included in the archive, a T4 template that demonstrate its usage; it’s easy to instantiate the object:

 IServiceProvider hostServiceProvider = (IServiceProvider)this.Host;  SettingsManager config = new SettingsManager(hostServiceProvider);

Then it’s easy to read the configuration from the project:

 this.WriteLine("Namespace : {0}", config.Namespace);
 this.WriteLine("AssemblyName: {0}", config.AssemblyName);
 this.WriteLine("ProjectPath : {0}", config.ProjectPath);
 this.WriteLine("ConfigPath : {0}", config.ConfigPath);
 this.WriteLine("Is App : {0}", config.IsApplication);
 this.WriteLine("Is WebApp : {0}", config.IsWebApplication);

 // Display each ConnectionStringSettings.
 foreach (ConnectionStringSettings connectionstringentry in config.ConnectionStrings) {
   this.WriteLine("Name: {0}, ConnectionString: {1}, ProviderName: {2}", connectionstringentry.Name, connectionstringentry.ConnectionString, connectionstringentry.ProviderName);

 // Display each KeyValueConfigurationElement.
 foreach (KeyValueConfigurationElement keyValueElement in config.AppSettings) {    this.WriteLine("Key: {0}, Value: {1}", keyValueElement.Key, keyValueElement.Value);



Hope you find the template useful…

del.icio.us Tags: ,

George Capnias is working as Software Architect and Development Manager. His developing career started as soon as he left high-school and got a job as a programmer for a small software house in Athens. He started developing with Microsoft’s products as soon as Visual Basic for Windows came out. Since September '93, he was member in one of the greater BBS in Athens, where he served as a moderator for Windows related forums till '98. He started developing Web applications in '96. September '99 he started working as a trainer for Compact SA (Microsoft Gold Certified Partner for Learning Solutions - CPLS). He is certified as Microsoft Certified Professional & Site Building (MCP+SB), Microsoft Certified Solution Developer (MCSD) for Visual Studio 6 and Visual Studio 2003, Microsoft Certified Database Administrator (MCDBA) for SQL Server 2000, Microsoft Certified Technology Specialist (MCTS) for Windows & Web Applications, Microsoft Certified Professional Developer (MCPD) for Windows & Web Applications and he has the status of Microsoft Certified Trainer (MCT). He is the leader of dotNETZone.gr, an Athens, Greece, user group for Greek software developers using Microsoft’s .NET platform. He is delivering technical presentations and workshops for Microsoft Hellas to audiences like developers, ISVs. He was awarded as MVP for 'Visual Studio and Development Technologies' in '16 and '17, for 'ASP.NET/IIS technologies' in '06 and '15 and as MVP for 'VSTO technologies' in '07, '08, '09, '10, '11, '12, '13 and '14.

Tagged with: ,
Posted in T4 Templates
6 comments on “T4 templates using own project .config file
  1. Hong Shen says:

    How can I get the zip file from http://cid-1a746c4e01342b70.skydrive.live.com/embedrowdetail.aspx/T4Templates/SettingsManager.zip? The URL gives back “This item won’t load right now”.

    Hong Shen

  2. You can try the link SettingsManager.zip instead!

    George J.

  3. […] You cannot do it directly as T4 runs under a different context than that of the project. See T4 templates using own project .config file or Accessing app.config/web.config from T4 template – Take 2 for additional information on […]

  4. […] and regular config code won’t work here. For this I used the SettingsManager class available here. Note: Although this code works you can’t debug it in Visual Studio 2012 because it fails […]

  5. […] You cannot do it directly as T4 runs under a different context than that of the project. See T4 templates using own project .config file, T4 templates for Web.config/App.config AppSettings or T4 Template For AppSettings Access In Config […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: