.NET 控制台应用程序中可以有多个 App.Config 文件吗?
我有一个包含 App.Confile 文件的控制台应用程序。 现在,此处维护特定于环境的参数。
现在我正在考虑拥有多个app.config文件(例如app.dev.config、app.test.config和app.prod.config)有多个 Web.Config 文件。
对于 Web 应用程序,我们可以处理这个问题,ConfigurationManager 将选择相应的 Web.Config 文件。
对于控制台应用程序,我不确定。如果是,我们如何拥有多个 app.config 文件?
感谢您的帮助。
谢谢
I have a console application that has App.Confile file.
Now the parameters that are environment specific are maintained here.
Now I am thinking to have multiple app.config files (like app.dev.config, app.test.config and app.prod.config) the way how we can have multiple Web.Config files.
In case of Web application, we can handle this and ConfigurationManager would pick respective Web.Config file.
In case of Console application, I am not sure. If Yes, how can we have multiple app.config files?
Appreciate your help.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了遵循 Babak 的答案,您还可以通过在表示
ConfigurationSection
的任何元素上使用configSource
属性,将部分配置分离到其他配置文件中,例如:在appSettings.config中:
To follow on from Babak's answer, you could also separate parts of your config out into other config files by using the
configSource
attribute on any element which represents aConfigurationSection
, e.g.:And in appSettings.config:
更新
使用Visual Studio 2010和2012,这一切都已集成到IDE中。如果右键单击配置文件,VS 会提供为每个构建配置生成转换配置的选项。如果您要为每个环境创建构建配置,MSBuild 将自动为您生成正确的 Web.config/app.config。
简短回答,是的。您可以在构建脚本中使用不同的文件,但必须将正确的文件重命名为“App.config”,然后就可以了(在编译之前)。
长答案,您应该使用的是 Enterprise Library MergeConfiguration 工具。这允许您使用现有的 App.config 作为基础并定义每个环境的增量。该工具将合并基础和增量以生成特定于环境的配置文件。您仍然需要构建脚本中的一些逻辑来应用正确的配置文件。
当您在计算机上安装 Enterprise Library 时,您可以右键单击 Visual Studio 中的配置文件并通过配置工具对其进行编辑。您可以使用它来定义您的环境以及应用程序设置和连接字符串以覆盖每个环境。
http://entlib.codeplex.com/
UPDATE
With Visual Studio 2010 and 2012, you this has all been integrated into the IDE. If you right click your config file, VS give you the option to generate a transform config for each of your build configurations. If you were to create a build configuration for each of your environments, MSBuild will automatically generate the correct Web.config/app.config for you.
Short answer, yes. You can have the different files and in your build script, but you'll have to rename the correct one to "App.config" and you're set (before you compile).
Long answer, what you should be using is the Enterprise Library MergeConfiguration tool. This allows you to use your existing App.config as the base and define deltas per environment. The tool will merge the base and the delta to generate the environment-specifig config files. You will still need some logic in a build script to apply the correct config file.
When you install Enterprise Library on your machine, you can right click the config file in Visual Studio and edit it via the config tool. You can use that to define your environments and the app settings and connection strings to override per environment.
http://entlib.codeplex.com/