简体   繁体   English

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

[英]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文件。

新的Visual C#控制台应用程序

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.

相关问题 如何从应用程序级别使用Azure中的服务配置? - How can I use service configuration in Azure from application level? 如何在C#控制台应用程序中使用配置替换 - How Can I Use Configuration Replacements for C# Console Application 如何使解决方案中的所有项目都使用单个应用程序配置文件? - How can I make all projects in a solution use a single application configuration file? 如何将数据传递给 csproj,或者我可以在 C# .netFramework 项目中的新构建配置上使用#if 指令 - How can I pass data to csproj or can I use #if directive on a new build configuration in C# .netFramework project 如何在.net应用程序中共享应用程序配置? - How can I share application configuration in a .net application? 如何在应用程序存储中存储多个值 - How can I store several values in Application storage 如何将文件放入安装项目中用户的本地应用程序数据文件夹中? - How can I put files in the user's local application data folder in a setup project? 如何在一个应用程序中使用多个.rpt文件(水晶报表)? - How can you use multiple .rpt files (crystal reports) in one application? 如何为多个结构制作一段代码? - How can I make one piece of code for several structs? 如何在WCF服务和客户端配置文件中禁用安全性 - How Can I Disable Security In WCF Service And Client Configuration Files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM