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

吃可爱长大的小学妹 提交于 2019-12-18 10:49:11

问题


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

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

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


回答1:


Unload project in the solution explorer via contet menu. Edit .csproj file. Add this strings into file.

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



回答2:


I have recently posted a supremely belated response to a similar SO topic: 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.transform approach for app.config files. (i.e. it makes use of the namespace 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.

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 x number of config files which get copied in their entirety, such as in other answers.

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


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




回答3:


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

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 .Settings files as debug settings, that are still linked to the App.config. The App.release.config must be build by hand, but it's pretty easy to switch this functionality.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/1295497/visual-studio-differentiate-app-config-for-debug-and-release-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!