C# Code, Tutorials and Full Visual Studio Projects

App.config and Configurations

Posted by on Jan 10, 2011 in Tips & Tricks | 4 comments

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 2010.





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.

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.

4 Comments

Join the conversation and post a comment.

  1. 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”

  2. 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.

  3. 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

  4. thomas

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

Leave a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>