The Grayzone

Changing app.config for Build Configurations

I’m currently working on an application where config data (connection strings etc) is stored machine encrypted, using the Windows Data Protection API, in app.config.

The Problem

In the standard app.config file all strings are encrypted for my development machine. I also wanted to store them encrypted for the deployment machine and automatically publish these values when building a release version of the application.

The Solution

The solution was to alter the csproj/vbproj file, adding an ItemGroup with a condition to deploy a different app.config depending on the build configuration.

First I added another app.config file within a ‘Release’ folder. I then edited the csproj file. To do this you can either:

  1. Use a text editor (Notepad, Notepad++, Wordpad etc).
  2. In Visual Studio, right click -> Unload Project. Right click -> Edit.

Within the csproj file you will find the following XML within the main Project node:

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

Remove this tag and replace it with the following:

<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
  <None Include="App.config" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
  <None Include="Release\App.config" />
</ItemGroup>

This tells MSBuild to use App.config when using Debug configuration, or Release\App.config when using Release configuration.


Share this: