Jarloo

Menu

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" ?>
<!--DEBUG-->
<configuration>
  <appSettings>
    <add key="MySetting" value="debugValue"/>
  </appSettings>
</configuration>

Now add this to app.config.Release

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

 

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

Comments

  • Posted: July 7, 2011 04:55

    Dani

    Thanks for cheat code, but the this command dump an error if your path contains spaces or the file "App.config" already exits. This command correct this: copy /Y "$(ProjectDir)App.config.$(ConfigurationName)" "$(ProjectDir)App.config"
    • Posted: November 10, 2011 18:03

      admin

      Seems to work fine for me when the app.config file is there, it just replaces it. Haven't tried it with spaces in the path name.
  • Posted: January 12, 2012 05:49

    Ronnie

    You can remove App.config and use only App.config.Release and App.config.Debug. Then add this as a post-build command. copy $(ProjectDir)App.config.$(ConfigurationName) $(ProjectDir)\bin\$(ConfigurationName)\$(ProjectName).exe.config
  • Posted: February 20, 2012 09:14

    thomas

    Thanks, just the tip I needed. So far it works fine for me and overwrites the app.config as described.