我已經在我的App.Config中設置了一些自定義配置部分,這樣我現在有了一個像這樣的configSection。如何閱讀configSections
<configSections>
<section name="Section1" type="ConfigSections.MySection, MyNamespace"/>
<section name="Section2" type="ConfigSections.MySection, MyNamespace"/>
<section name="Section3" type="ConfigSections.MySection, MyNamespace"/>
</configSections>
我想要做的就是閱讀代碼本節爲了在運行時發現我有什麼部分。我曾嘗試過:
var mySections = ConfigurationManager.GetSection("configSections");
但是這會返回null。我確信我錯過了一些簡單的事情,但是我找不到如何做到這一點的任何事情。
感謝
它認爲你必須這樣做:var mySections = ConfigurationManager.GetSection(「Section1」); – HW90 2012-04-26 10:47:35
爲了澄清,我非常高興如何閱讀「Section1」部分。我正在嘗試閱讀「configSections」部分,因此我不必在代碼中對文本「Section1」進行硬編碼。原因是我在運行時不知道我將會擁有多少節。 – bornfromanegg 2012-04-26 11:13:37
換句話說,我希望能夠讀取我的App.Config中「configSections」元素(上面)中包含的每個「section」元素的「name」屬性。 (這很難解釋!) – bornfromanegg 2012-04-26 11:15:29