在很多时候我们需要自定义我们自己的自定义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. 解决Ruby在IE11中报Unable to get browser (Selenium::WebDriver::Error::NoSuchWindowError)的错误

    转载地址:http://www.tuicool.com/articles/BRnqeu2 I was updating the browser WebDrivers for    Seleno    ...

  2. 【转】PowerShell入门(六):远程操作

    转至:http://www.cnblogs.com/ceachy/archive/2013/02/20/PowerShell_Remoting.html PowerShell远程操作是远程管理的基础, ...

  3. 引用参数,值参数,ref,out

    1,一个参数只有在引用的时候才能改变其值,这是一种情况 2,一个参数在引用后要永久的改变其值(可以用返回参数的形式) 3,多个参数在引用后要永久的改变其值或者多个参数中的部分(返回参数就适合了,因为只 ...

  4. Uva 11542 乘积是平方数

    题目链接:http://vjudge.net/contest/142484#problem/A 这个题目也是2016年CCPC网赛上面的题目,当时我是不会做的,但是大牛们都知道这是一个原题,最后给一队 ...

  5. Python对整形数字进行加密和解密

    # -*- coding:utf-8 -*- __author__ = 'Ray' class Encryption: """整形数字简单的一个加密/解密算法" ...

  6. [问题2015S07] 复旦高等代数 II(14级)每周一题(第八教学周)

    [问题2015S07]  设 \(A\) 为 \(n\) 阶复方阵, 证明: 存在 \(n\) 阶非异复对称阵 \(S\), 使得 \(A'=S^{-1}AS\), 即 \(A\) 可通过非异复对称阵 ...

  7. 用JavaBean实现数据库的连接和关闭,在jsp页面输出数据库中student表中学生的信息

    package com.hanqi.test; import java.sql.*; public class XveSheng { Connection conn; Statement st; Re ...

  8. 3. Swift 数组|字典|集合

    在OC中数组,字典,集合有自己的表示方法,分别是Array,Dictionary,Set 与 String 都属于数值类型变量,他们都属于结构体. 使用简介灵活多变,个人感觉可读性变差了很多,用起来由 ...

  9. IP和端口的相关检测

    1.查看自己电脑的ip,使用ipconfig命令 2.检测某个ip是否可以连通,直接使用ping命令 3.windows下查看本机都有哪些端口开放,使用netstat -anp tcp 命令 4.查看 ...

  10. Composite模式

    1 意图:将对象组成树形结构,以表示“部分——整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 2 动机:同意处理图元对象和包含图元的容器对象.Composite通过 ...