根据编译器常量更改配置设置
Alter configuration settings based on compiler constants
在代码中,我知道根据通过编译器常量反映的当前活动构建配置来包含或排除某些代码部分,例如 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>
我该怎么做?
你不能这样做,配置不提供确切的功能。
不过,您可以使用的是配置转换。使用它,您可以为每个构建配置定义一个可选的转换,您可以在其中执行如下操作:
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?。
在代码中,我知道根据通过编译器常量反映的当前活动构建配置来包含或排除某些代码部分,例如 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>
我该怎么做?
你不能这样做,配置不提供确切的功能。
不过,您可以使用的是配置转换。使用它,您可以为每个构建配置定义一个可选的转换,您可以在其中执行如下操作:
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?。