App.config and Configurations

Does this scenario sound familiar: You have an app with lots of settings in your app.config file that works great for production, but you have different settings that you need for development?

I don’t know why Microsoft hasn’t made this an easier process, but there is a fairly simple low tech way to make this happen right in Visual Studio.

How this works

You create multiple app.config files that have your configuration specific settings. Then you simply add a build event to Visual Studio that takes the right app.config file and overwrites the main app.config with the settings you specify.

So if you have two configurations: Debug and Release. You would make an app.config, app.config.Debug and app.config.Release. Put your debug settings in the debug one and your release settings in the release one. Then when the project is built as Release, Visual Studio will take the app.config.Release and overwrite app.config with it.

This will ensure that when you build your application you will also have the correct settings.

Keep following for an example:

Create Multiple app.config Files

First add three app.config files to your project. Call them:

  • app.config
  • app.config.Debug
  • app.config.Release

Leave app.config blank and add the following to app.config.Debug:

<?xml version="1.0" encoding="utf-8" ?>
    <add key="MySetting" value="debugValue"/>

Now add this to app.config.Release

<?xml version="1.0" encoding="utf-8" ?>
    <add key="MySetting" value="releaseValue"/>


Visual Studio Project Settings

Right-Click your project and select Properties.

Goto the Build Events tab and in the Pre-build box put this:

copy $(ProjectDir)App.config.$(ConfigurationName) $(ProjectDir)App.config

Now build your project. Notice as you flip configurations from Release to Debug and rebuild your project that the app.config file changes to have either your Debug or Release settings.

[box type=”warning”]You may need to right-click your project and select Rebuild instead of a regular build. Most of the time a regular build works just fine, but sometimes a full rebuild is needed. This seems to be a bug with Visual Studio 2010.[/box]

Categories:   Code


Sorry, comments are closed for this item.