0

In code, I know to include or exclude certain sections of code based on the currently active build configuration reflected through compiler constants, such as DEBUG:

static void Main()
{
    #if DEBUG

        //While debugging this section is used.

    #else

        //In Release this section is used. This is the "normal" way.

    #endif 
}

Now I want to do the same in a configuration file, such as web.config or app.config, something like this:

<appSettings>
<!-- IF DEBUG -->
    <add key="foo" value="debug-setting" />
<!-- ELSE -->
    <add key="foo" value="release-setting" />
<!-- ENDIF -->
</appSettings>

How can I do that?

CodeCaster
  • 131,656
  • 19
  • 190
  • 236
Riddhi
  • 184
  • 4
  • 16
  • What do you mean by "this type of setting"? Doesn't [Easier way to debug a C# Windows Service](http://stackoverflow.com/questions/125964/easier-way-to-debug-a-c-sharp-windows-service) answer your question? – CodeCaster Jun 13 '16 at 15:14
  • I mean to check if project is in debug mode or release mode which I clearly mentioned above. – Riddhi Jun 13 '16 at 15:16
  • Repeating what you said doesn't make your question any clearer. Do you mean that you want `` in your app.config? You want to [use configuration transformation for that](http://stackoverflow.com/questions/3004210/app-config-transformation-for-projects-which-are-not-web-projects-in-visual-stud). – CodeCaster Jun 13 '16 at 15:16
  • Thanks for your answer (may be it will work) and thanks for decreasing my question but not all developers in this world are same. – Riddhi Jun 13 '16 at 15:32
  • My point is that your question was unclear. It may be very clear in your head what you want, but writing it so that others also understand that is hard. I've tried to edit your question so it shows what I think you mean, and have answered that question. – CodeCaster Jun 13 '16 at 15:35

1 Answers1

0

You can't do that, configurations don't provide that exact functionality.

What you can use though, is configuration transformation. Using that, you define an optional transform per build configuration, where you can do something like this:

App.config:

<appSettings>
    <add key="foo" value="debug-setting" />
</appSettings>

App.Release.config:

<appSettings>
    <add key="foo" value="release-setting" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>

See also App.Config Transformation for projects which are not Web Projects in Visual Studio 2010?.

Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236