20

Is it possible to add conditions in app.config file?

I do below in the C# code and I want to do something similar in my app.config file too.

#if (Debug)
.......
#else
.....
#endif
Justin
  • 84,773
  • 49
  • 224
  • 367
CharithJ
  • 46,289
  • 20
  • 116
  • 131

3 Answers3

11

You can try some type of app.config transforms like this: http://fknut.blogspot.com/2009/11/appconfig-transformation-with-new.html.

Or have multiple app.config files like app.config.debug and app.config.release, and use the appropriate one in your build.

mellamokb
  • 56,094
  • 12
  • 110
  • 136
4

That is not possible, although in Visual Studio 2010 there is a feature that lets you apply configuration-dependent transformations to the Web.config file, but only when publishing using msdeploy.

There is no such transformation for executable applications. You might want to look into using a post-build action to copy a release or debug config file.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • For executable application the same can now be achived with https://marketplace.visualstudio.com/items?itemName=vscps.SlowCheetah-XMLTransforms – Jens Aug 08 '18 at 13:39
2

You should be able to do something like this programatically, by putting the relevant sections in two different config files, then at app-start time, load the appropriate config file. The majority of your config would be in the main app.config, and there would be a second config file for the rest.

I don't have the syntax offhand, but it's basically the same idea as having a user.config with user-specific configurations.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136