我如何可以使用多个应用程序配置文件在一个项目? [英] How can I use several Application Configuration Files in one project?

查看:193
本文介绍了我如何可以使用多个应用程序配置文件在一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建新的Visual C#控制台应用程序(.NET框架4.5)后,这样的项目包含默认App.config文件。

After creating new Visual C# Console Application (.NET Framework 4.5), such project contains default App.config file.

增加了对System.Configuration项目,在一些源文件使用System.Configuration使用它的引用之后; 我可以使用静态类 ConfigurationManager中与App.config文件进行操作。但在此之前,我想一些设置添加到该文件,因此它在某种程度上是这样的:

After adding a reference for System.Configuration to project and use it in some source file using System.Configuration; I can use static class ConfigurationManager to operate with App.config file. But before, I want to add some settings to the file, so it's somehow like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="DeployWeb" value="true" />
  </appSettings>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

现在,我可以写这样的事情,让设置的值:

Now, I can write something like this, to get the value of the setting:

Boolean deployWeb = false;
Boolean.TryParse(ConfigurationManager.AppSettings["DeployWeb"], out deployWeb);



不过我没有设置读取哪个配置文件,但没关系,因为默认一。但我可以通过右键单击该项目添加更多配置文件 - >添加 - >新项... - >应用程序配置文件,使我有,例如,5配置文件,如图片上的:

However I didn't set which configuration file to read,but it's okay, because there is the default one. But I can add more configuration files by right click on the project -> Add -> New Item... -> Application Configuration File, so that I have, for example, 5 configuration files, like on the picture:

ConfigurationManager中仍然会读取默认的,但我想手动控制,要使用的配置文件。我想知道,如果有合适的方法来设置为 ConfigurationManager中使用等config文件名,如果它不是一个不好的做法。我知道如何使用调试/发布模式不同的配置,但在我来说,我有一个应用程序,可在不同的目的而发布,例如不同的模式下拼命地跑。

And ConfigurationManager will still read the default one, but I want to manually control, which config file to use. I want to know, if is there an appropriate way to set to the ConfigurationManager config file name to use etc. and if it's not a bad practice. I know how to use different configurations in debug/release mode, but in my case I have an application, that can be runned in different modes for different purposes in release, for example.

:是否有可能在一个项目几个配置文件,并有转我想用哪一种能力。这难道不是一种不好的做法,我将用一些我的目的另一种方法?使用生成事件不适合我的情况(我认为)。

Question: Is it possible to have several configuration files in a project and to have ability to switch which one I want to use. Isn't it a bad practice, shall I use some another approach for my purpose ? Using build events is not suitable in my case (I think).

PS。我很抱歉伸展我的问题,但我没戏相当简单,可以只问两句话,但作为规则说,问题应该包含的细节。

PS. I am sorry for stretching my question, however my itis quite simple and could be just asked in two sentences, but as the rules says, question should contains details.

更新
从已经的现有的应答选项:您可以使用ConfigurationManager中通过类代码装入备用配置文件。从阅读 MSDN 我没有得到其中,我应该使用的方法。我应该打开埃克配置?

UPDATE: From already existing answer "Option: You can use the ConfigurationManager Class to load an alternate config file by code." From reading msdn I didn't get which of the methods should I use. Should I open Exe configuration ?

推荐答案

它可以通过映射配置文件来完成,

It can be done using mapped config file ,

ExeConfigurationFileMap configFileMap = 
        new ExeConfigurationFileMap();
    configFileMap.ExeConfigFilename = "App4.config"; // full path to the config file

    // Get the mapped configuration file
   Configuration config = 
      ConfigurationManager.OpenMappedExeConfiguration( 
        configFileMap, ConfigurationUserLevel.None);

   //now on use config object

AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");

这篇关于我如何可以使用多个应用程序配置文件在一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆