2013-02-19 53 views
10

這裏是我提出的想法:如何通過名稱獲取所有部分在sectionGroup的applicationSettings .NET 2.0中

我想要一個小型可執行文件有屬於sectionGroup下位於多個部分的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上的項目(當前位於this commit)可能很有趣。

回答

21

看看ConfigurationManager.OpenExeConfiguration函數加載你的配置文件。

然後在System.Configuration.Configuration類,你會從ConfigurationManager.OpenExeConfiguration回來,你會想看看SectionGroups財產。這將返回一個ConfigurationSectionGroupCollection其中你會發現applicationSettings部分。

ConfigurationSectionGroupCollection會有一個Sections屬性,它包含了ExecutableFirstModuleConfigurationSection對象。

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

你會希望得到ConfigurationSectionGroupCollection對象或ConfigurationSection對象後檢查null。如果它們爲空,它們不存在於配置文件中。

您還可以通過使用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; 
} 
+0

幫助很大,非常感謝你。我會給兩個直接引用我的例子的upvotes,但我不能... :( – HaMster 2013-02-19 19:43:57

+0

沒問題,很高興幫助,你可能想看看我的最新編輯,我包含了一些關於'ConfigurationManager.GetSection'的信息。如果您不需要對配置文件執行任何其他操作,通常是訪問配置文件的首選方式。 – 2013-02-19 20:26:18

+6

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); - 這將打開當前配置沒有必要知道EXE路徑 – timothy 2014-09-12 08:36:52

相關問題