Home > Uncategorized > Configuration Transformation in Windows/Console Apps

Configuration Transformation in Windows/Console Apps


If you are an ASP.NET Web Developer you might have seen the advantages of Web.Config Transformations. Here you can have separate Config files for each configuration defined in “Application Configuration Manager”

image image

But the Configuration Transformations are not supported for Windows Or Console Applications. But there is an work-around way to get it done. Lets see How it is…by Step by Step.



You can observe that this has only one App.Config file. Lets add two configuration files “App.Test.Config”, “App.Prob.Config”


Newly added files are at the Root Level of the Project instead of under App.Config file. See how they are defined in .proj file. (For this you need unload the project and Edit the Project File in Visual Studio)


Use DependentUpon  element to make “App.Test.Config”, “App.Prob.Config” files Childs to “App.Config” as  shows below…


Reload the Project and See the result


Let Add a connection String to the App.Config file.

<?xml version="1.0"?>



        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>




    <add name="MyConnString" connectionString="Data Source=.;Initial Catalog=MyDataBase_dev;Integrated Security=True" providerName="System.Data.SqlClient" />




Add Connection strings to Child Config files also… (with the change in database name MyDataBase_Prod, MyDataBase_Test)

<?xml version="1.0" encoding="utf-8" ?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">


  <connectionStrings xdt:Transform="Replace">

    <add name="MyConnString" connectionString="Data Source=.;Initial Catalog=MyDataBase_Prod;Integrated Security=True" providerName="System.Data.SqlClient" />




To know more about the Transformation syntax refer below links


Now…Let Test it by Setting The Project Configuration to Test and Build…


If you look at the config file at bin\Test folder…you will find connection string same as “App.config” file.


Out of the box, .config file transformation is only available for Web application projects in Visual Studio 2010. Console, WinForms, WPF and other applications projects don’t support this capability out of the box.

What is the Work-Around? TransformXml Task

In this MSBuild code snippet, we are using the TransformXml task defined in the Microsoft.Web.Publishing.Tasks.dll. This is the task responsible for transforming Web.config files in Web application projects. It takes parameters that specify names of the original .config file, the transform file and the output (transformed) file. We use TransformXml task to override AfterCompile target if a transform file exist for the current build configuration. For example, if our project contains a file called App.Debug.config and the current project configuration is Debug, the AfterCompile target will be called. Finally, we use ItemGroup element to change the built-in AppConfigWithTargetPath item collection.

Add Below code to the Project file.

  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

  <Target Name="AfterCompile" Condition="exists(‘app.$(Configuration).config’)">

    <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />


      <AppConfigWithTargetPath Remove="app.config" />

      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">





That’s it….!

Build the project in Test Configurations and see the output @ bin\Test folder…!

Hope this Helps..Smile

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

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: