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.
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 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:
- Use a text editor (Notepad, Notepad++, Wordpad etc).
- 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.