visual-studio Visual Studio:针对调试和发布模式区分 app.config

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1295497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:52:56  来源:igfitidea点击:

Visual Studio: differentiate app.config for debug and release mode

visual-studioapp-configproduction-environment

提问by Korey

Is there a way to automatically use a separate app.config when building in release mode?

在发布模式下构建时,有没有办法自动使用单独的 app.config?

In other words, I want to test with one app.config, and release with another.

换句话说,我想用一个 app.config 进行测试,然后用另一个发布。

Currently, I keep a separate copy called app.config.production, and manually overwrite bin\Release\Application.exe.config after building for release.

目前,我保留一个名为 app.config.production 的单独副本,并在构建发布后手动覆盖 bin\Release\Application.exe.config。

回答by trueboroda

Unload the project in Solution Explorer via the context menu.

通过上下文菜单在解决方案资源管理器中卸载项目。

Edit the .csprojfile via the context menu and add this:

.csproj通过上下文菜单编辑文件并添加以下内容:

<PropertyGroup>
    <AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>

回答by ne1410s

I have recently posted a supremely belated response to a similar SO topic: https://stackoverflow.com/a/27546685/2798367

我最近发布了对类似 SO 主题的极其迟到的回复:https: //stackoverflow.com/a/27546685/2798367

I will repeat it here for clarity:

为清楚起见,我将在这里重复一遍:

This is somewhat late to the party, but I stumbled upon a nice way of implementing the web.transformapproach for app.configfiles. (i.e. it makes use of the namespace http://schemas.microsoft.com/XML-Document-Transform)

这对派对来说有点晚了,但我偶然发现了一种实现文件web.transform方法的好方法app.config。(即它使用命名空间http://schemas.microsoft.com/XML-Document-Transform

I think it is "nice" because it is a pure xml approach and doesn't require 3rd party software.

我认为它“很好”,因为它是一种纯 xml 方法,不需要 3rd 方软件。

A parent / default App.config file is descended from, according to your various build configurations. These descendants then only override what they need to. In my opinion this is much more sophisticated and robust than having to maintain xnumber of config files which get copied in their entirety, such as in other answers.

根据您的各种构建配置,父/默认 App.config 文件是从属的。这些后代然后只覆盖他们需要的东西。在我看来,这比必须维护x完整复制的配置文件数量(例如在其他答案中)要复杂和强大得多。

A walkthrough has been posted here: http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/

演练已在此处发布:http: //mitasoft.wordpress.com/2011/09/28/multipleappconfig/



Look, Mom - No explicit post-build events in my IDE!

看,妈妈 - 我的 IDE 中没有明确的构建后事件!

回答by Martin Braun

A simple and fast way is to create a second file "App.release.config" and insert this pre-build event:

一个简单快捷的方法是创建第二个文件“App.release.config”并插入这个预构建事件:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.config" "$(ProjectDir)App.debug.config"
IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.release.config" "$(ProjectDir)App.config"

And this post build event:

而这个后期构建事件:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.debug.config" "$(ProjectDir)App.config"

This might be a bit odd, but it will allow you to keep using the .Settingsfiles as debug settings, that are still linked to the App.config. The App.release.configmust be build by hand, but it's pretty easy to switch this functionality.

这可能有点奇怪,但它允许您继续使用这些.Settings文件作为调试设置,这些文件仍然链接到App.config. 在App.release.config必须建立手工,但它很容易切换此功能。

回答by Rudy Hinojosa

I highly recommend SlowCheetah for app.config transformations. Visit this nuget gem here Visual Studio Gallery

我强烈推荐 SlowCheetah 用于 app.config 转换。在此处访问此 nuget gem Visual Studio Gallery

回答by Dave

I don't know if this helps, but app.config will recognise the standard MSBUILD substitution strings such as $(Configuration).

我不知道这是否有帮助,但 app.config 会识别标准的 MSBUILD 替换字符串,例如 $(Configuration)。

回答by Mark Zhukovsky

Similar to top answer but with this approach you can see the actual file if preferred and intellisense doesn't complain in csproj file:

与顶级答案类似,但如果使用这种方法,您可以看到实际文件,如果首选并且智能感知不会在 csproj 文件中抱怨:

  <Target Name="SetAppConfig" BeforeTargets="Compile">
    <Copy SourceFiles="debug.config" DestinationFiles="app.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " />
    <Copy SourceFiles="release.config" DestinationFiles="app.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
  </Target>