根据编译器常量更改配置设置

时间:2016-06-13 15:13:22

标签: c# debugging web-config app-config

在代码中,我知道根据编译器常量反映的当前活动构建配置包含或排除某些代码段,例如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 
}

现在我想在配置文件中执行相同的操作,例如web.config或app.config,如下所示:

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您不能这样做,配置不能提供确切的功能。

您可以使用的是配置转换。使用它,您可以为每个构建配置定义一个可选的转换,您可以在其中执行以下操作:

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>

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