如何在.Net 2.0的sectionGroup applicationSettings中按名称获取所有部分

How to get all sections by name in the sectionGroup applicationSettings in .Net 2.0(如何在.Net 2.0的sectionGroup applicationSettings中按名称获取所有部分)

本文介绍了如何在.Net 2.0的sectionGroup applicationSettings中按名称获取所有部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的想法:

我想要一个小的可执行文件有一个 app.config 文件,其中包含位于 sectionGroupapplicationSettings"(不是appSettings",我不需要写入文件)下的多个部分.每个部分都有一个对应于模块的名称,如果设置了该模块,则应该加载该模块.

I want a small executable to have an app.config file with multiple sections that are situated under the sectionGroup "applicationSettings" (not "appSettings", I don't need to write to the file). Each section would have a name corresponding to a module that should be loaded if set.

这是一个例子:

   <configuration>
     <configSections>
       <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
         <section name="Executable" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
         <section name="FirstModule" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
       </sectionGroup>
     </configSections>
     <applicationSettings>
       <Executable>
         <setting name="MyFirstSetting" serializeAs="String">
           <value>My awesome feature setting</value>
         </setting>
       </Executable>
       <FirstModule path="path to the modules assembly">
         <setting name="ImportantSettingToTheModule" serializeAs="String">
           <value>Some important string</value>
         </setting>
       </FirstModule>
     </applicationSettings>
   </configuration>

现在,如果我定义 FirstModule 部分,我希望我的应用程序加载它的程序集.如果未定义该部分,则不应加载该模块.这不仅适用于一个模块,而且还适用于尚未定义的模块数量.

Now if I define the FirstModule section, I want my application to load its assembly. If the section is not defined, the module should not be loaded. This should be true for not only one module but a not yet defined number of them.

所以我基本上需要在运行时找出定义的部分.我该怎么做?

So I basically need to find out about the defined sections at runtime. How would I do that?

此外,我希望它成为向后兼容 .NET 2.0 的可移植可执行文件(= 它也必须在 Mono 上运行).

In addition I want this to become a portable executable (= it has to run on Mono as well) that is backwards compatible to .NET 2.0.

看看 GitHub 上的项目可能会很有趣(目前在 这个提交).

It might be interesting to have a look at the project on GitHub (currently at this commit).

推荐答案

看看ConfigurationManager.OpenExeConfiguration 函数加载到你的配置文件中.

Take a look at the ConfigurationManager.OpenExeConfiguration function to load in your configuration file.

然后在 System.Configuration.Configuration 类'将从 ConfigurationManager.OpenExeConfiguration 中返回,您需要查看 SectionGroups 属性.这将返回一个 ConfigurationSectionGroupCollection 您将在其中找到 applicationSettings 部分.

Then on the System.Configuration.Configuration class that you'll get back from ConfigurationManager.OpenExeConfiguration you'll want to look at the SectionGroups property. That'll return a ConfigurationSectionGroupCollection in which you'll find the applicationSettings section.

ConfigurationSectionGroupCollection 中会有一个 Sections 属性,其中包含 ExecutableFirstModule ConfigurationSection 对象.

In the ConfigurationSectionGroupCollection there will be a Sections property which contains the Executable and FirstModule ConfigurationSection objects.

var config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);
var applicationSettingSectionGroup = config.SectionGroups["applicationSettings"];
var executableSection = applicationSettingSectionGroup.Sections["Executable"];
var firstModuleSection = applicationSettingSectionGroup.Sections["FirstModule"];

您需要在获得 ConfigurationSectionGroupCollection 对象或 ConfigurationSection 对象后检查 null.如果它们为 null,则它们不存在于配置文件中.

You will want to check for null after getting the ConfigurationSectionGroupCollection object or ConfigurationSection objects. If they are null they don't exist in the configuraiton file.

您还可以使用 <代码>ConfigurationManager.GetSection

You can also get the sections by using ConfigurationManager.GetSection

var executableSection = (ClientSettingsSection)ConfigurationManager
    .GetSection("applicationSettings/Executable");
var firstModuleSection = (ClientSettingsSection)ConfigurationManager
    .GetSection("applicationSettings/FirstModule");

同样,如果对象为 null,则它们不存在于配置文件中.

Again, if the objects are null they don't exist in the configuration file.

要获取您可以执行的所有部分名称和组的列表:

To get a list of all the section names and groups you could do:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// use the line below instead if you want to load an app.config for a
//   different application other than the one the code is running in
// var config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);

var names = new List<string>();
foreach (ConfigurationSectionGroup csg in config.SectionGroups)
    names.AddRange(GetNames(csg));

foreach (ConfigurationSection cs in config.Sections)
    names.Add(cs.SectionInformation.SectionName);

private static List<string> GetNames(ConfigurationSectionGroup configSectionGroup)
{
    var names = new List<string>();

    foreach (ConfigurationSectionGroup csg in configSectionGroup.SectionGroups)
        names.AddRange(GetNames(csg));

    foreach(ConfigurationSection cs in configSectionGroup.Sections)
        names.Add(configSectionGroup.SectionGroupName + "/" + cs.SectionInformation.SectionName);

    return names;
}

这篇关于如何在.Net 2.0的sectionGroup applicationSettings中按名称获取所有部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在.Net 2.0的sectionGroup applicationSettings中按名称获取所有部分

基础教程推荐