如何获得通过名称的所有部分在sectionGroup的applicationSettings在.net 2.0 [英] How to get all sections by name in the sectionGroup applicationSettings in .Net 2.0

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

问题描述

下面是我有了想法:

我想要一个小的可执行文件具有与那些下sectionGroup的applicationSettings位于多个部分的app.config文件(不的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向后兼容。

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 将有一个包含属性可执行 FirstModule 配置节 对象

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 对象或配置节的对象。如果他们是空,他们不会在configuraiton文件存在。

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");



此外,如果对象是他们。不要在配置文件中不存在

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

这篇关于如何获得通过名称的所有部分在sectionGroup的applicationSettings在.net 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆