-->

如何获得通过名称的所有部分在sectionGroup的applicationSettings在.ne

2019-07-20 04:33发布

下面是我的想法:

我想一个小的可执行文件具有与被下sectionGroup“的applicationSettings”位于多个部分的app.config文件(不是“的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部分,我想我的应用程序加载其组装。 如果没有定义的部分,该模块不应该被加载。 这应该不是只有一个模块但是尚未确定他们的数量是真实的。

所以基本上,我需要了解在运行时所定义的部分。 我会怎么做呢?

此外,我希望这成为一个可移植可执行(=它具有单声道以及运行),也就是.NET 2.0向后兼容。

这可能是有趣的,看看在GitHub上的项目(目前在此提交 )。

Answer 1:

看看在ConfigurationManager.OpenExeConfiguration功能配置文件来加载。

然后在System.Configuration.Configuration类,你会得到从后ConfigurationManager.OpenExeConfiguration你要看看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"];

您将要检查null让后ConfigurationSectionGroupCollection对象或ConfigurationSection对象。 如果它们是空,他们不会在configuraiton文件存在。

您也可以通过使用得到部分ConfigurationManager.GetSection

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

再次,如果对象是null他们没有在配置文件中存在。

要得到所有你能做的部分名称和组的列表:

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


文章来源: How to get all sections by name in the sectionGroup applicationSettings in .Net 2.0