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

本文介绍了一种使用XML格式的配置文件结构,详细展示了如何通过.NET Framework中的ConfigurationManager获取配置信息,并解释了NameValueSectionHandler与DictionarySectionHandler的区别及用法。
<?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/archive/2011/04/28/2032126.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值