在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的

System.Configuration.DictionarySectionHandler

System.Configuration.NameValueSectionHandler      

System.Configuration.SingleTagSectionHandler

DictionarySectionHandler使用

  DictionarySectionHandler的工作方式与NameValueFileSectionHandler几乎相同,其区别是DictionarySectionHandler返回HashTable对象,而后者返回的是NameValueCollection。

 <configSections>
<section name="mailServer" type="System.Configuration.DictionarySectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<mailServer>
<add key="url" value="mail.163.com"/>
<add key="username" value="admin"/>
<add key="password" value="123456"/>
</mailServer>

  使用代码

 IDictionary dic = ConfigurationManager.GetSection("mailServer") as IDictionary;
Console.WriteLine(dic);
foreach (var key in dic.Keys)
{
Console.WriteLine("{0}:{1}", key, dic[key]);
}
Console.ReadKey();

由于DictionarySectionHandler返回的是HashTable对象,而HashTable中的Key是唯一的。那么DictionarySectionHandler如果配置了相同的Key,后面的值会覆盖前面的值。

还是上面的的例子,我们将配置文件修改一下

 <mailServer>
<add key="url" value="mail.163.com"/>
<add key="username" value="admin"/>
<add key="password" value="123456"/>
<add key="password" value="12345678"/>
</mailServer>

接下来看看输出结果:

NameValueSectionHandler使用

  xml配置

 <configSections>
<section name="mailServer" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<mailServer>
<add key="url" value="mail.163.com"/>
<add key="username" value="admin"/>
<add key="password" value="123456"/>
<add key="password" value="12345678"/>
</mailServer>

  代码:

 NameValueCollection mailServer = ConfigurationManager.GetSection("mailServer") as NameValueCollection;
Console.WriteLine(mailServer);
foreach (var key in mailServer.AllKeys)
{
Console.WriteLine("{0}:{1}",key,mailServer[key]);
}
Console.ReadKey();

  输出结果:

SingleTagSectionHandler使用

  SingleTagSectionHandler和DictionarySectionHandler一样,同样返回的是Hashtable对象,只是书写结构不一样。

  xml配置:

 <configSections>
<section name="mailServer" type="System.Configuration.SingleTagSectionHandler,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<mailServer url="mail.163.com" username="admin" password="12345678"/>

  代码:

 IDictionary mailServer = ConfigurationManager.GetSection("mailServer") as Hashtable ;
Console.WriteLine(mailServer);
foreach (var key in mailServer.Keys)
{
Console.WriteLine("{0}:{1}", key, mailServer[key]);
}
Console.ReadKey();

  输出结果:

c# 配置文件之configSections配置(二)的更多相关文章

  1. c# 配置文件之configSections配置

    对于小型项目来说,配置信息可以通过appSettings进行配置,而如果配置信息太多,appSettings显得有些乱,而且在开发人员调用时,也不够友好,节点名称很容易写错,这时,我们有几种解决方案 ...

  2. c# 配置文件之configSections配置(三)

    使用IConfigurationSectionHandler配置configSections ·步骤1:定义一个实体类 using System; using System.Collections.G ...

  3. 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置

    第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本  uwsgi- ...

  4. Mybatis系列(二):优化MyBatis配置文件中的配置和解决字段名与实体类属性名不相同的冲突

    原文链接:http://www.cnblogs.com/xdp-gacl/p/4264301.html     http://www.cnblogs.com/xdp-gacl/p/4264425.ht ...

  5. App.config和Web.config配置文件的自定义配置节点

    前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...

  6. (转)struts2.0配置文件、常量配置详解

    一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...

  7. 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置

    [转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...

  8. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  9. Windows Redis默认配置文件,Redis配置不生效解决方案

    Windows Redis默认配置文件,Redis配置不生效解决方案, Windows Redis自启动配置不生效解决方案,Windows Redis增加自动启动服务 >>>> ...

随机推荐

  1. PowerDesigner15.1给自定义架构表字段添加MS_Description出错

    原因:系统函数sp_addextendedproperty 的第3个参数(用户名) 应该是Schema.但PD在生成的时候却是’user’ 解决方法 在PDM时.DataBase >> E ...

  2. BJFU 1068

    描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结 ...

  3. CPU cache

    cache是一种小而快的缓冲器,用在CPU和main memory之间进行数据读写. 在processor访问主memory时,首先检查cache中是不是有一份copy,如果cache hit,则直接 ...

  4. UCenter整合登陆时出现’Authorization has expired’错误(2014-03-13记)

    原因之可能是两台机子的时间不一致导致.

  5. IntelliJ IDEA使用(2)——IDEA配置Tomcat

    如果网上流传的方法(即方法2)不能配置成功,点击加号什么都没有的话,请看方法一配置方法. 解决问题:intlellij IDEA配置tomcat点击加号没东西. 方法一:手动添加tomcat插件然后再 ...

  6. 2016年12月19日 星期一 --出埃及记 Exodus 21:14

    2016年12月19日 星期一 --出埃及记 Exodus 21:14 But if a man schemes and kills another man deliberately, take hi ...

  7. 网页设计中常用的19个Web安全字体

    来自http://www.jb51.net 在Web编码中,CSS默认应用的Web字体是有限的,虽然在新版本的CSS3,我们可以通过新增的@font-face属性来引入特殊的浏览器加载字体.但多数情况 ...

  8. [问题2014S09] 解答

    [问题2014S09]  解答 充分性:  先证明对 Jordan 块 \(J_r(1)\) 以及任意的正整数 \(m\), 均有 \(J_r(1)^m\) 相似于 \(J_r(1)\). 设 \(N ...

  9. WP8没有开发者账号升级GDR3预览版的方法

    很多希望尝鲜WP8 GDR3的朋友一样希望提前体验GDR3的效果,但苦于没有开发者账号而不得其门!今天为大家带来普通账号升级GDR3的方法! 工具/原料 WP8智能手机 可用wifi热点 方法/步骤 ...

  10. hdu1242 优先队列+bfs

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...