Multiple app.config in C# Solution based on Build Selection

Hi,

This is a nifty trick which I have learned in the recent months, so I thought to share …

In few projects I came across, I observed there is been a trend where based on the project build settings the application config settings can be tweaked, as commonly found in ASP MVC applications template.

So how to achieve it in almost any C# project, here the trick presented:

1. Create a C# Console Application

2. Add a two new “App.Config” files as follows

3. Now its time to “Unload” our project for some manual edits to the solution project files with favourite text editor, or in VS it open your “project.csproj” file for edit

4. Now make the following edits in the file “project.csproj”

Replace Edit
From:

  <ItemGroup>
    <None Include="App.config" />
    <None Include="App.Debug.config" />
    <None Include="App.Release.config" />
  </ItemGroup>

To:

  <ItemGroup>
    <None Include="App.config" />
    <None Include="App.Debug.config">
      <DependentUpon>App.config</DependentUpon>
    </None>
    <None Include="App.Release.config">
      <DependentUpon>App.config</DependentUpon>
    </None>
  </ItemGroup>

Add section of xml below the highlighted line in the code below

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
    <!-- Generate transformed app config in the
           intermediate directory -->
    <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
    <!-- Force build process to use the transformed configuration
           file from now on. -->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="app.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

5. Voila and once you reload the project post the above changes you can see the following

6. And now for the usage, following should be the content of the corresponding config files

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <appSettings>
    <add key="Build" value="None Build"/>
  </appSettings>
</configuration>

App.Debug.config
(Note: Please ensure the xmlns,xml name space is added to the following files)

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="Build" value="Debug Build" xdt:Transform="Replace" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

App.Release.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="Build" value="Release Build" xdt:Transform="Replace" xdt:Locator="Match(key)" />  
  </appSettings>
</configuration>

And that’s all, now as you select you build type and check the corresponding build config you will obtain the following result.

Debug

Release

Hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

*