2013-02-19 55 views
10

यहाँ में sectionGroup applicationSettings में नाम से सभी वर्गों को पाने के लिए विचार मैं था है:कैसे नेट 2.0

मैं एक छोटे से निष्पादन चाहते sectionGroup के तहत स्थित हैं कि कई वर्गों के साथ एक app.config फ़ाइल के लिए " एप्लिकेशन सेटिंग्स "(नहीं" ऐप सेटिंग्स ", मुझे फ़ाइल में लिखने की आवश्यकता नहीं है)। प्रत्येक खंड में एक मॉड्यूल से संबंधित नाम होगा जिसे सेट किया जाना चाहिए।

<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 के पीछे संगत है।

गिटहब (वर्तमान में this commit पर) पर प्रोजेक्ट को देखना दिलचस्प हो सकता है।

उत्तर

21

अपनी कॉन्फ़िगरेशन फ़ाइल में लोड करने के लिए ConfigurationManager.OpenExeConfiguration फ़ंक्शन पर नज़र डालें।

फिर System.Configuration.Configuration कक्षा पर आप ConfigurationManager.OpenExeConfiguration से वापस आ जाएंगे, तो आप SectionGroups संपत्ति को देखना चाहेंगे। वह ConfigurationSectionGroupCollection लौटाएगा जिसमें आपको applicationSettings अनुभाग मिलेगा।

ConfigurationSectionGroupCollection में एक Sections संपत्ति जो Executable और FirstModuleConfigurationSection वस्तुएं शामिल किया जाएगा।

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

आप ConfigurationSectionGroupCollection वस्तु या ConfigurationSection वस्तुओं मिलने के बाद null के लिए जाँच करना चाहते हैं जाएगा। अगर वे शून्य हैं तो वे 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; 
} 
+0

यह एक बहुत मदद की, आपको बहुत बहुत धन्यवाद। मैं सीधे अपने उदाहरण का जिक्र करने के लिए दो अपवॉट्स दूंगा, लेकिन मैं नहीं कर सकता ... :( – HaMster

+0

कोई समस्या नहीं, मदद करने में खुशी हुई, आप मेरा नवीनतम संपादन देखना चाहेंगे। मैंने 'ConfigurationManager.GetSection' पर कुछ जानकारी शामिल की है। यदि आपको कॉन्फ़िगरेशन फ़ाइल पर कोई अन्य क्रिया करने की आवश्यकता नहीं है, तो आमतौर पर कॉन्फ़िगरेशन फ़ाइल तक पहुंचने का पसंदीदा तरीका होता है। –

+6

कॉन्फ़िगरेशन कॉन्फ़िगरेशन = ConfigurationManager.OpenExe कॉन्फ़िगरेशन (कॉन्फ़िगरेशनउसर लेवल.None); - यह वर्तमान के लिए कॉन्फ़िगरेशन खोल देगा एप। exe पथ जानने की जरूरत नहीं है। – timothy

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^