C# 如何在 .Net 2.0 中的 sectionGroup applicationSettings 中按名称获取所有部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14963870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all sections by name in the sectionGroup applicationSettings in .Net 2.0
提问by HaMster
Here's the idea I had:
这是我的想法:
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.
我想要一个小的可执行文件有一个 app.config 文件,其中包含位于 sectionGroup“applicationSettings”(不是“appSettings”,我不需要写入文件)下的多个部分。每个部分都有一个与模块对应的名称,如果设置了该模块应该被加载。
Here's an example:
下面是一个例子:
<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>
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.
现在,如果我定义 FirstModule 部分,我希望我的应用程序加载它的程序集。如果未定义该部分,则不应加载该模块。这不仅适用于一个模块,还适用于尚未定义的数量。
So I basically need to find out about the defined sections at runtime. How would I do that?
所以我基本上需要在运行时找出定义的部分。我该怎么做?
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.
此外,我希望它成为向后兼容 .NET 2.0 的可移植可执行文件(= 它也必须在 Mono 上运行)。
It might be interesting to have a look at the project on GitHub (currently at this commit).
在 GitHub 上查看该项目可能会很有趣(目前在此提交中)。
采纳答案by JG in SD
Take a look at the ConfigurationManager.OpenExeConfiguration
function to load in your configuration file.
查看ConfigurationManager.OpenExeConfiguration
要加载到配置文件中的函数。
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.
然后在System.Configuration.Configuration
你回来的课堂上,你ConfigurationManager.OpenExeConfiguration
会想看看这个SectionGroups
属性。这将返回一个ConfigurationSectionGroupCollection
您将在其中找到该applicationSettings
部分的。
In the ConfigurationSectionGroupCollection
there will be a Sections
property which contains the Executable
and FirstModule
ConfigurationSection
objects.
在 中将ConfigurationSectionGroupCollection
有一个Sections
包含Executable
和FirstModule
ConfigurationSection
对象的属性。
var config = ConfigurationManager.OpenExeConfiguration(pathToExecutable);
var applicationSettingSectionGroup = config.SectionGroups["applicationSettings"];
var executableSection = applicationSettingSectionGroup.Sections["Executable"];
var firstModuleSection = applicationSettingSectionGroup.Sections["FirstModule"];
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.
您将需要null
在获取一个ConfigurationSectionGroupCollection
或多个ConfigurationSection
对象后进行检查。如果它们为空,则它们不存在于配置文件中。
You can also get the sections by using ConfigurationManager.GetSection
您还可以使用 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.
同样,如果这些对象null
在配置文件中不存在。
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;
}