自定义应用程序配置文件3(app.config)

 
sp;
            //访问配置节Test2
            IDictionary IDTest2 = (IDictionary)ConfigurationSettings.GetConfig("Test2");
            string[] keys=new string[IDTest2.Keys.Count];
            string[] values=new string[IDTest2.Keys.Count];
            IDTest2.Keys.CopyTo(keys,0);
            IDTest2.Values.CopyTo(values,0);
            MessageBox.Show(keys[0]+" "+values[0]);
           
            //访问配置节Test3
            NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3");
            MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]);    //输出Hello World

通过上面的代码我们可以看出,不同的type通过GetConfig返回的类

型不同,具体获得配置内容的方式也不一样。 配置节处理程序
 返回类型
 
SingleTagSectionHandler
 Systems.Collections.IDictionary
 
DictionarySectionHandler
 Systems.Collections.IDictionary
 
NameValueSectionHandler
 Systems.Collections.Specialized.NameValueCollection
 

  3.2 自定义配置节组
配置节组是使用<sectionGroup>元素,将类似的配置节分到同一个组中。配置节组声明部分将创建配置节的包含元素,在<configSections>元素中声明配置节组,并将属于该组的节置于<sectionGroup>元素中。下面是一个包含配置节组的配置文件的例子:

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="TestGroup">
            <section name="Test" type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
    </configSections>
   
    <TestGroup>
        <Test>
            <add key="Hello" value="World"/>
        </Test>
    &

lt;/TestGroup>
</configuration>
 下面是访问这个配置节组的代码:
            NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("TestGroup/Test");
            MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]);    //输出Hello World

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值