查看要加载到配置文件中的ConfigurationManager.OpenExeConfiguration
函数。
这是我的想法:
我希望一个小的可执行文件具有一个app.config文件,该文件具有位于sectionGroup“ 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上查看项目可能很有趣(当前位于this commit。] >>
这里是我的想法:我希望一个小的可执行文件具有一个app.config文件,该文件具有位于sectionGroup“ applicationSettings”(不是“ appSettings”,我不需要...]下的多个节。 >
查看要加载到配置文件中的ConfigurationManager.OpenExeConfiguration
函数。
然后从ConfigurationManager.OpenExeConfiguration
返回的System.Configuration.Configuration
类上,您需要查看System.Configuration.Configuration
属性。这将返回一个ConfigurationManager.OpenExeConfiguration
,您将在其中找到SectionGroups
部分。
在ConfigurationSectionGroupCollection
中将有一个ConfigurationSectionGroupCollection
属性,其中包含applicationSettings
和ConfigurationSectionGroupCollection
Sections
对象。
Executable
获得
FirstModule
对象或ConfigurationSection
对象后,您将要检查ConfigurationSection
。如果它们为null,则它们在配置文件中不存在。您也可以使用
var config = ConfigurationManager.OpenExeConfiguration(pathToExecutable); var applicationSettingSectionGroup = config.SectionGroups["applicationSettings"]; var executableSection = applicationSettingSectionGroup.Sections["Executable"]; var firstModuleSection = applicationSettingSectionGroup.Sections["FirstModule"];
来获得这些部分>null
同样,如果对象为
ConfigurationSectionGroupCollection
,则它们在配置文件中不存在。要获取所有节名称和组的列表,可以执行以下操作:
ConfigurationSection
查看要加载到配置文件中的ConfigurationManager.OpenExeConfiguration
函数。