.config 自定义配置节 【转载】

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>    
    <section name="EncryptionMethod"  type="System.Configuration.DictionarySectionHandler, System,  Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" restartOnExternalChanges="false" />   <!--此处配置信息注意写全-->

    <section name="Mod"  type="System.Configuration.DictionarySectionHandler, System,  Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" restartOnExternalChanges="false" />  <!--此处配置信息注意写全-->
    
    <sectionGroup name="mySectionGroup"> <!--多了组的概念-->
      <section name="mySection" type="System.Configuration.NameValueSectionHandler" />         
    </sectionGroup>

 </configSections>

  <EncryptionMethod>
      <add key="Decrypt" value = "0"/>
      <add key="Encrypt" value = "1"/>
  </EncryptionMethod>

  <Mod>
    <add key="a" value = "0"/>
    <add key="b" value = "1"/>    
  </Mod>

  <mySectionGroup>
    <mySection>
      <add key="c" value = "0"/>
      <add key="d" value = "1"/>
    </mySection>    
  </mySectionGroup>

</configuration>


    //使用 NameValueSectionHandler 或 DictionarySectionHandler 类的自定义配置节定义设置
    //NameValueCollection 的读取
    NameValueCollection config = (NameValueCollection)ConfigurationManager.GetSection("mySectionGroup/mySection");
    //DictionarySectionHandler 的读取 
    IDictionary dict = ConfigurationManager.GetSection("EncryptionMethod") as IDictionary;
DictionarySectionHandler的工作方式与NameValueFileSectionHandler几乎相同,其区别是DictionarySectionHandler返回HashTable对象,而不是Object。
原文地址:https://www.cnblogs.com/haibin168/p/2032126.html