如何在一个项目中使用多个应用程序配置文件?

时间:2021-08-29 06:56:41

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

创建新的Visual C#控制台应用程序(.NET Framework 4.5)后,此类项目包含默认的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:

将System.Configuration的引用添加到项目并使用System.Configuration在某个源文件中使用它;我可以使用静态类ConfigurationManager来操作App.config文件。但之前,我想在文件中添加一些设置,所以它有点像这样:

<?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);

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:

但是我没有设置要读取的配置文件,但是没关系,因为有默认配置文件。但是我可以通过右键单击项目 - >添加 - >新项目... - >应用程序配置文件来添加更多配置文件,以便我有例如5个配置文件,如图所示:

如何在一个项目中使用多个应用程序配置文件?

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.

ConfigurationManager仍然会读取默认值,但我想手动控制使用哪个配置文件。我想知道,是否有适当的方法来设置ConfigurationManager配置文件名使用等,如果这不是一个坏的做法。我知道如何在调试/发布模式下使用不同的配置,但在我的情况下,我有一个应用程序,例如,可以在不同的模式下运行,以便在发布时用于不同的目的。

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. 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.

PS。我很抱歉伸出我的问题,但是我很简单,可以用两句话来问,但正如规则所说,问题应该包含细节。

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 ?

更新:从现有的答案“选项:您可以使用ConfigurationManager类通过代码加载备用配置文件。”从阅读msdn我没有得到我应该使用哪种方法。我应该打开Exe配置吗?

1 个解决方案

#1


10  

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");

#1


10  

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");