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

本文关键字:获取 applicationSettings Net sectionGroup 中的 | 更新日期: 2023-09-27 18:00:19

这是我的想法:

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

这里有一个例子:

   <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部分,我希望我的应用程序加载它的程序集。如果未定义节,则不应加载模块。这不仅适用于一个模块,而且适用于尚未定义数量的模块。

因此,我基本上需要在运行时了解已定义的部分。我该怎么做?

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

看看GitHub上的项目可能会很有趣(目前正在提交中)。

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

查看要加载到配置文件中的ConfigurationManager.OpenExeConfiguration函数。

然后,在您将从ConfigurationManager.OpenExeConfiguration返回的System.Configuration.Configuration类上,您将希望查看SectionGroups属性。这将返回一个ConfigurationSectionGroupCollection,您将在其中找到applicationSettings部分。

ConfigurationSectionGroupCollection中将有一个Sections属性,它包含ExecutableFirstModule ConfigurationSection对象。

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

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

您也可以使用ConfigurationManager.GetSection 获取部分

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

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

要获得所有分区名称和组的列表,您可以执行以下操作:

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;
}