c# 配置文件之configSections配置(二)
在很多时候我们需要自定义我们自己的自定义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配置(二)的更多相关文章
- c# 配置文件之configSections配置
对于小型项目来说,配置信息可以通过appSettings进行配置,而如果配置信息太多,appSettings显得有些乱,而且在开发人员调用时,也不够友好,节点名称很容易写错,这时,我们有几种解决方案 ...
- c# 配置文件之configSections配置(三)
使用IConfigurationSectionHandler配置configSections ·步骤1:定义一个实体类 using System; using System.Collections.G ...
- 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置
第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本 uwsgi- ...
- Mybatis系列(二):优化MyBatis配置文件中的配置和解决字段名与实体类属性名不相同的冲突
原文链接:http://www.cnblogs.com/xdp-gacl/p/4264301.html http://www.cnblogs.com/xdp-gacl/p/4264425.ht ...
- App.config和Web.config配置文件的自定义配置节点
前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...
- (转)struts2.0配置文件、常量配置详解
一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...
- 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置
[转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- Windows Redis默认配置文件,Redis配置不生效解决方案
Windows Redis默认配置文件,Redis配置不生效解决方案, Windows Redis自启动配置不生效解决方案,Windows Redis增加自动启动服务 >>>> ...
随机推荐
- 移动前端中viewport(视口) 转
移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dpi ( device-independent pixel 设备逻辑像素 )和 CSS 像素之间 ...
- 上传图片预览设置src不显示
使用滤镜效果解决: window.parent.document.body.focus(); var imgurl = (document.selection.createRange().text). ...
- linux 如何开机自动运行sh脚本
vi /etc/rc.d/rc.local #自动启动oracleecho 502 >/proc/sys/vm/hugetlb_shm_group su - oracle -c 'sh /dat ...
- php多维数组去除空元素
在php中去除数组中的空值可以使用array_filter() 这个函数 但是这个函数只能对一维数组起作用,一旦需要对多维数组去空就不行了,而且去除的空也包括(int)0,(string)0,使用起来 ...
- 配置eclipse集成开发环境_编译_调试
1. 因为eclipse是基于Java运行,所以在运行Eclipse之前,需要安装Java SE,对于Java SE,需要Java SE6 JRE系列的版本,可以在这个位置下载: Java SE 6只 ...
- touch穿透
出现穿透的前提:上层绑定touch事件,下层绑定click事件. 解决方案:touchend绑定: e.preventDefault();$(下层).css("pointer-events& ...
- 从容而优雅(leisurely and elegant)
每时每刻, 我都变得更好了. ----- 法国心理学家 埃米尔 . 库埃 每时每刻, 我都变得更忙了. ----- 罗伯特 . 西奥迪尼 咬牙切齿的寒风, 昏暗的路灯, 默默的走过那一段从教室到寝 ...
- 《BI项目笔记》增量ETL数据抽取的策略及方法
增量抽取 增量抽取只抽取自上次抽取以来数据库中要抽取的表中新增或修改的数据.在ETL使用过程中.增量抽取较全量抽取应用更广.如何捕获变化的数据是增量抽取的关键.对捕获方法一般有两点要求:准确性,能够将 ...
- linux chomd 学习
chomd -R 777 directory_name :递归地给directory目录下所有文件和子目录的属主分配读的权限 ------2016-10-31 -- source: Linux chm ...
- velocity常用语句速查表
velocity常用语句 * 变量定义 #set($directoryRoot = "www" ) * #if($!list.size() != 0) //判断list不为空 #f ...