我可以在app.config文件中添加条件吗?

问题描述 投票:15回答:4

是否可以在app.config文件中添加条件?

我在下面的C#代码中做了,我想在我的app.config文件中做类似的事情。

#if (Debug)
.......
#else
.....
#endif
c# .net app-config configuration-files
4个回答
10
投票

你可以尝试某种类型的app.config变换:http://fknut.blogspot.com/2009/11/appconfig-transformation-with-new.html

或者有多个app.config文件,如app.config.debugapp.config.release,并在您的构建中使用适当的文件。


4
投票

这是不可能的,尽管在Visual Studio 2010中有一个功能允许您将configuration-dependent transformations应用于Web.config文件,但仅在使用msdeploy发布时。

可执行应用程序没有这样的转换。您可能希望使用后构建操作来复制发布或调试配置文件。


1
投票

您应该能够以编程方式执行此类操作,方法是将相关部分放在两个不同的配置文件中,然后在应用程序启动时加载相应的配置文件。你的大部分配置都在主app.config中,剩下的就是第二个配置文件。

我没有随意的语法,但它与具有用户特定配置的user.config基本相同。


0
投票

由于某种原因,我无法下面工作。似乎不是呐

<Choose>
    <When Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
      <connectionStrings configSource="App.Debug.config"/>
    </When>
    <Otherwise>
        <connectionStrings configSource="App.Debug.config"/>
    </Otherwise>
  </Choose>
© www.soinside.com 2019 - 2024. All rights reserved.