[英]How can I use several Application Configuration Files in one project?
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;
将System.Configuration的引用添加到项目并使用System.Configuration在某个源文件中使用它using System.Configuration;
I can use static class ConfigurationManager
to operate with App.config file. 我可以使用静态类ConfigurationManager
来操作App.config文件。 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);
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. ConfigurationManager
仍会读取默认值,但我想手动控制使用哪个配置文件。 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. 我想知道,如果有适当的方法设置ConfigurationManager
配置文件名使用等,如果这不是一个坏的做法。 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. 我很抱歉伸出我的问题,但是我很简单,可以用两句话来问,但正如规则所说,问题应该包含细节。
UPDATE : From already existing answer "Option: You can use the ConfigurationManager Class to load an alternate config file by code." 更新 :从现有的答案 “选项:您可以使用ConfigurationManager类通过代码加载备用配置文件。” From reading msdn I didn't get which of the methods should I use. 从阅读msdn我没有得到我应该使用哪种方法。 Should I open Exe configuration ? 我应该打开Exe配置吗?
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.