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

此生再无相见时 提交于 2019-11-28 03:15:35

问题


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


回答1:


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/




回答2:


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, e.g.:

<appSettings configSource="appSettings.config" />

And in appSettings.config:

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


来源:https://stackoverflow.com/questions/4464148/can-we-have-multiple-app-config-files-in-net-console-application

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