C# 在开发过程中管理多个应用配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/176061/
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
Manage multiple app config files during development
提问by Robert Rossney
I'm building an application that is used by several different customers. Each customer has a fair amount of custom business logic, which I have cleverly refactored out into an assembly that gets loaded at runtime. The name of that assembly, along with a number of other customer-specific settings, are stored in the application's configuration file.
我正在构建一个由多个不同客户使用的应用程序。每个客户都有相当数量的自定义业务逻辑,我巧妙地将其重构为在运行时加载的程序集。该程序集的名称以及许多其他客户特定的设置都存储在应用程序的配置文件中。
Right now, here's what I have to do in order to debug the application for customer foo:
现在,为了调试客户 foo 的应用程序,我必须执行以下操作:
- Go to the filesystem in my project directory and delete
app.config
- Copy
app.config.foo
toapp.config.foo - Copy
. - Rename
app.config.foo - Copy
asapp.config
. - Tell Windows that yes, I want to change the file's extension.
- Switch back to Visual Studio.
- Open the
Settings.settings
item in my project. - Click "Yes" 13 or 14 times as VS asks me if I want to use the new settings that have been changed in
app.config
. - Close
Settings.settings
.
- 转到我项目目录中的文件系统并删除
app.config
- 复制
app.config.foo
到app.config.foo - Copy
. - 重命名
app.config.foo - Copy
为app.config
. - 告诉 Windows 是的,我想更改文件的扩展名。
- 切换回 Visual Studio。
Settings.settings
在我的项目中打开该项目。- 当 VS 询问我是否要使用在
app.config
. - 关闭
Settings.settings
。
Okay! Now I'm ready to debug!
好的!现在我准备调试了!
It seems to me that the rigamarole of opening Settings.settings
is, or ought to be, unnecessary: I don't need the default values in Settings.cs
to be regenerated, because I don't use them. But it's the only way I know of to make VS aware of the fact that the app.config
file has changed, so that the build will copy it to the output directory.
在我看来,打开的繁琐Settings.settings
是不必要的,或者应该是不必要的:我不需要Settings.cs
重新生成默认值,因为我不使用它们。但这是我所知道的让 VS 知道app.config
文件已更改这一事实的唯一方法,以便构建将其复制到输出目录。
There's got to be an easier way of doing this. What is it?
必须有一种更简单的方法来做到这一点。它是什么?
采纳答案by Robert Rossney
A couple of people suggested using multiple VS configurations, which I think would have worked, except that it would require me to rebuild the solution every time I switched between configurations.
有几个人建议使用多个 VS 配置,我认为这会奏效,只是每次在配置之间切换时都需要我重新构建解决方案。
What I did instead seemed a little stupid while I was doing it, but I've been using it for nearly a year now and it works extremely smoothly. In my project directly, I create a separate app.config.XXX
file for each customer. The actual app.config
file is used solely to generate Settings.cs
- it has all of the correct setting names and their default values. It doesn't get copied to the build directories, ever.
在我做的时候,我所做的似乎有点愚蠢,但我已经使用它将近一年了,它运行得非常顺利。直接在我的项目中,我app.config.XXX
为每个客户创建了一个单独的文件。实际app.config
文件仅用于生成Settings.cs
- 它具有所有正确的设置名称及其默认值。它永远不会被复制到构建目录中。
Then I wrote a little program that lets me select a customer, and that simply goes through the directories for each project and, if I selected customer XXX, copies app.config.XXX
to bin\debug\myprogram.exe.config
and bin\release\myprogram.exe.config
. As long as this program knows where the root of the solution is (I have to be a little careful whenever I branch the code), it works like a charm.
然后我写了一个小程序,让我选择一个客户,它简单地遍历每个项目的目录,如果我选择了客户 XXX,则复制app.config.XXX
到bin\debug\myprogram.exe.config
和bin\release\myprogram.exe.config
。只要这个程序知道解决方案的根在哪里(每当我对代码进行分支时我都必须小心一点),它就像一个魅力。
回答by Gulzar Nazim
You can refer this post for some good practices : Managing Multiple Configuration File Environments with Pre-Build Events
您可以参考这篇文章了解一些好的做法:使用预构建事件管理多个配置文件环境
回答by icelava
You may opt to define multiple Visual Studio solution configurations, one for each customer, and have customised MSBuild targets for your Windows app project.
你可以选择定义多个 Visual Studio 解决方案配置,每个客户一个,并为你的 Windows 应用项目自定义 MSBuild 目标。
I have documented the steps of how I handled this here. Multiple app.config files for deploying to different environments
我在这里记录了我如何处理这个问题的步骤。用于部署到不同环境的多个 app.config 文件
回答by magravelle
You can also let Visual Studio automate Robert`s approach by:
您还可以通过以下方式让 Visual Studio 自动化 Robert 的方法:
- Define a Build Configuration for each client
- In the post build event, simply xcopy app.config.xxx to your bin folder. Where XXX is the name of a Build Config accessible in VS. Something like: xcopy app.config.$(ConfigurationName) $(OutDir)/app.config
- 为每个客户端定义构建配置
- 在后期构建事件中,只需将 app.config.xxx 复制到您的 bin 文件夹。其中 XXX 是可在 VS 中访问的构建配置的名称。类似于: xcopy app.config.$(ConfigurationName) $(OutDir)/app.config
VS will drop a distinct build for your clients in separate folders, aolong with the proper config file. bin/Client1/ bin/Client2/
VS 将在单独的文件夹中为您的客户端放置一个不同的构建,以及正确的配置文件。bin/Client1/ bin/Client2/
回答by Rafael
Thinking about the mess of managing multiple configuration files I made this tool: http://envride.codeplex.com/
考虑管理多个配置文件的混乱,我制作了这个工具:http: //envride.codeplex.com/
Its purpose its exactly to make it easier to manage multiple configuration files
in an automated way. I would be very pleased if you would take a look at it.
它的目的正是为了更容易地configuration files
以自动化方式管理多个。如果你能看一看,我会很高兴的。
回答by Sebastian Castaldi
After a little digging and work around I got my Test project working with multiple configurations,
经过一些挖掘和工作,我让我的测试项目可以使用多种配置,
- In the Configuration Manager, create the configurations you need
- Copy paste your app.config and add the name of the configuration, in my case is AHI, FIV, MGC, so my config files look like: App.AHI.config, App.MGC.config, App.FIV.Config. You can name it how ever you wanted, but keep the same convention
- Add a Post-Build event. In my case it would look like: xcopy $(ProjectDir)app.$(ConfigurationName).config $(TargetDir)$(TargetName).dll.config /y
- 在配置管理器中,创建您需要的配置
- 复制粘贴你的 app.config 并添加配置的名称,在我的例子中是 AHI、FIV、MGC,所以我的配置文件看起来像:App.AHI.config、App.MGC.config、App.FIV.Config。您可以随意命名,但要保持相同的约定
- 添加构建后事件。在我的情况下,它看起来像: xcopy $(ProjectDir)app.$(ConfigurationName).config $(TargetDir)$(TargetName).dll.config /y
here is my post, so you can read it with more details
这是我的帖子,所以你可以阅读更多细节
回答by Menelaos Vergis
This thread is too old to represent current tools in VS.
此线程太旧,无法代表 VS 中的当前工具。
- You can use an addon that acts similar to web.debug.config but for app.config.
- 您可以使用类似于 web.debug.config 但用于 app.config 的插件。
https://marketplace.visualstudio.com/items?itemName=GolanAvraham.ConfigurationTransform
https://marketplace.visualstudio.com/items?itemName=GolanAvraham.ConfigurationTransform
- And for the same app.config transformations without addon.
- 对于没有插件的相同 app.config 转换。
https://www.linkedin.com/pulse/multi-appconfig-visual-studio-2017-benjamin-davis/
https://www.linkedin.com/pulse/multi-appconfig-visual-studio-2017-benjamin-davis/