自定义App.config

App.config是.net自带的配置文件,对它的自定义方法一直也没有去看一下。今天在同事郑学剑的激励和网络红人贾君鹏同学的感召下简单学习了一下。现将学习的一点粗浅的心得记录于下。
自定义的包括自定义节的定义和具体配置两部分。
      定义部分以<configSections>作根节点,并且此节点必须为App.config文件根节点下的第一个元素,其下可以包括<sectionGroup>、<section>两种节点,<sectionGroup>下可以包含<section>或子一级的<sectionGroup>。<sectionGroup>和<section>都需要包含一个name属性,这个属性将用于稍后的具体配置部分。
      <section>节点还应包含一个type属性,type属性可以为一下三个值:
      System.Configuration.NameValueSectionHandler
      System.Configuration.DictionarySectionHandler
      System.Configuration.SingleTagSectionHandler
      具体配置部分的节点名为前面定义的<sectionGroup>和<section>节点的name属性值,结构和定义部分一致。按type属性值的不同<section>节点中可以以不同的格式添加具体的配置信息。
      NameValueSectionHandler和DictionarySectionHandler添加的格式:
      <section><add key="setting" value="value"></add>...</section>
      SingleTagSectionHandler添加的格式:
      <section setting="value" ... />
      示例App.config 
<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
 <configSections>
    <sectionGroup name="mySectionGroup">
       <section name="mySection1" type="System.Configuration.NameValueSectionHandler"/>
        <section name="mySection2" type="System.Configuration.DictionarySectionHandler"/>
        <section name="mySection3" type="System.Configuration.SingleTagSectionHandler"/>
      </sectionGroup>
    </configSections> 
   <mySectionGroup>
     <mySection1>
       <add key="name" value="贾君鹏"></add>
       <add key="sender" value="你妈妈叫你"></add>
       <add key="action" value="回家吃饭"></add>
     </mySection1>
    <mySection2>
       <add key="name" value="贾君鹏"></add>
       <add key="sender" value="你妈妈叫你"></add>
       <add key="action" value="回家吃饭"></add>
     </mySection2>
     <mySection3 name="贾君鹏" sender="你妈妈叫你" action="回家吃饭"/>
   </mySectionGroup>
 </configuration>


      在代码中可以使用 System.Configuration.ConfigurationManager类中静态方法GetSection访问自定义的配置项,参数为具体Section的path,返回值为Object类型。但对不同的Section的类型(type)需要将结果转换为不同的类型。(要访问System.Configuration.ConfigurationManager类,需要在工程中添加System.Configuration的引用)
      System.Configuration.NameValueSectionHandler——〉System.Collections.Specialized.NameValueCollection  
      System.Configuration.DictionarySectionHandler——〉System.Collections.Hashtable
      System.Configuration.SingleTagSectionHandler—— 〉 System.Collections.IDictionary
      代码示例
 
  private void testButton_Click(object sender, EventArgs e)
          {
              NameValueCollection section1 = System.Configuration.ConfigurationManager.GetSection("mySectionGroup/mySection1") 
                 as System.Collections.Specialized.NameValueCollection;
              string name1 = section1["name"];
              string sender1 = section1["sender"];
              string action1 = section1["action"];
 
             Hashtable section2 = System.Configuration.ConfigurationManager.GetSection("mySectionGroup/mySection2") 
                 as System.Collections.Hashtable;
             string name2 = section2["name"].ToString();
             string sender2 = section2["sender"].ToString();
             string action2 = section2["action"].ToString();
 
             IDictionary section3 = System.Configuration.ConfigurationManager.GetSection("mySectionGroup/mySection3") 
                 as System.Collections.IDictionary;
             string name3 = section3["name"].ToString();
             string sender3 = section3["sender"].ToString();
            string action3 = section3["action"].ToString();
 
             this.showTextBox.Text = name1 + sender1 + action1 + "\r\n"
                 + name2 + sender2 + action2 + "\r\n"
                 + name3 + sender3 + action3 + "\r\n";
         }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值