简体   繁体   English

我们可以在.NET控制台应用程序中拥有多个App.Config文件吗?

[英]Can we have multiple App.Config files in .NET console application?

I have a console application that has App.Confile file. 我有一个具有App.Confile文件的控制台应用程序 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. 现在我想有多个app.config文件 (比如app.dev.config,app.test.config和app.prod.config )我们如何拥有多个Web.Config文件。

In case of Web application, we can handle this and ConfigurationManager would pick respective Web.Config file. 在Web应用程序的情况下,我们可以处理这个,ConfigurationManager将选择相应的Web.Config文件。

In case of Console application, I am not sure. 如果是控制台应用程序,我不确定。 If Yes, how can we have multiple app.config files? 如果是,我们如何拥有多个app.config文件?

Appreciate your help. 感谢您的帮助。

Thanks 谢谢

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 a ConfigurationSection , eg: 要继续Babak的回答,您还可以通过在表示ConfigurationSection任何元素上使用configSource属性将配置的各个部分分离到其他配置文件中,例如:

<appSettings configSource="appSettings.config" />

And in appSettings.config : appSettings.config中

<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
    <add key="Blah" value="Nim nim nim" />
</appSettings>

UPDATE UPDATE

With Visual Studio 2010 and 2012 , you this has all been integrated into the IDE. 使用Visual Studio 20102012 ,您已将其全部集成到IDE中。 If you right click your config file, VS give you the option to generate a transform config for each of your build configurations. 如果右键单击配置文件,VS会为您提供为每个构建配置生成转换配置的选项。 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. 如果要为每个环境创建构建配置,MSBuild将自动为您生成正确的Web.config / app.config。

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). 您可以在构建脚本中拥有不同的文件,但是您必须将正确的文件重命名为“App.config”并进行设置(在编译之前)。

Long answer , what you should be using is the Enterprise Library MergeConfiguration tool. 答案很长 ,您应该使用的是Enterprise Library MergeConfiguration工具。 This allows you to use your existing App.config as the base and define deltas per environment. 这允许您使用现有的App.config作为基础并定义每个环境的增量。 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. 在计算机上安装Enterprise Library时,可以右键单击Visual Studio中的配置文件,然后通过配置工具对其进行编辑。 You can use that to define your environments and the app settings and connection strings to override per environment. 您可以使用它来定义您的环境以及应用程序设置和连接字符串以覆盖每个环境。

http://entlib.codeplex.com/ http://entlib.codeplex.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM