1)App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="color"   type="System.Configuration.NameValueSectionHandler" />
   
 <section name="message" type="System.Configuration.DictionarySectionHandler"/>
    <section name="name"   type="System.Configuration.SingleTagSectionHandler"/>

  </configSections>
  <color>
    <add key="red"   value="#ff0000"/>
    <add key="green" value="#00ff00"/>
    <add key="blue"  value="#0000ff"/>
  </color>
  <message>
    <add key="welcome" value="你好,欢迎"/>
  </message>
  <name firstName="陈" lastName="明明"/>

</configuration>

对于自定义的配置节,应该先在 <configSections>中声明要配置的节与类型,如着色部分,接着,在后面定义要配置的具体内容,正如定义一个变量。

(2)配置节的访问

public static void Main(string[] args)
        {
            //get color
            NameValueCollection color = (NameValueCollection)ConfigurationManager.GetSection("color");
            foreach (String str in color.AllKeys) {
                Console.WriteLine(str+":"+color[str]);
            }
            //get message
            IDictionary message = (IDictionary)ConfigurationManager.GetSection("message");
            foreach (String str in message.Keys) {
                Console.WriteLine(str+":"+message[str]);
            }
            // get name
            IDictionary name = (IDictionary)ConfigurationManager.GetSection("name");
            foreach (String str in name.Keys)
            {
                Console.WriteLine(str + ":" + name[str]);
            }   
            //Console.WriteLine(name["firstName"]);
            Console.Read();
        }

转转 http://www.cnblogs.com/zengle_love/archive/2009/03/22/1419138.html

C# App.config 自定义 配置节的更多相关文章

  1. C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法

    App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...

  2. C# App.config 自定义 配置节 出现的问题:配置系统未能初始化

    C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案 新建C#项目,在app.config中添加了appSettings项,运行时出现&q ...

  3. App.Config自定义配置节点

    配置文件: <?xml version="1.0" encoding="utf-8"?> <configuration> <con ...

  4. C#如何使用和开发自定义配置节

    在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择.这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉.程序中的配置信息一般放在应用程序的app.c ...

  5. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  6. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  7. 使用 ConfigurationSection 创建自定义配置节

    我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...

  8. C#创建自定义配置节

    在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...

  9. 如何修改 app.config 的配置信息

    如何修改 app.config 的配置信息 收藏 最问这个问题的人有点多,其实 .Net 提供了这样的功能我们可以在 app.config 中 userSettings 节点中保存我们的应用程序设置信 ...

随机推荐

  1. api接口统一封装

    具体的接口api模块,例如authorization.js import axios from '../axiosWrapper' let prefix = process.env.API_ROOT ...

  2. 配置 http 反向代理

    [root@nginx ~]# cd /etc/nginx/ 1 [root@nginx nginx]# cp nginx.conf nginx.conf.bak #备份一个原配置文件 2 [root ...

  3. 三、Vue CLI-单页面

    一.单页面 代码如下: <template> <div class="header">{{title}}</div> </template ...

  4. web部署命令简单记录

    非 root 用户设置环境变量:在< .bash_profile >中设置 后台运行:nohup dosomething >> log.out & nginx 启动ng ...

  5. SpringCloud系列(一):Eureka 注册中心

    在演示spring cloud之前得要知道我们为什么需要微服务框架. 先讲讲我的经历,以前我们做项目时所有功能都写在一起,只是做了分层(模型,数据,业务),所有业务逻辑都写在业务层,刚开始还好,等时间 ...

  6. SpringBootMVC02——SpringDataJpa与ThymeLeaf

    大纲 - SpringDataJpa进阶使用- SpringDataJpa自定义查询- 整合Servlet.Filter.Listener- 文件上传- Thymeleaf常用标签 1.整合Servl ...

  7. Wannafly挑战赛22 B 字符路径 ( 拓扑排序+dp )

    链接:https://ac.nowcoder.com/acm/contest/160/B 来源:牛客网 题目描述 给一个含n个点m条边的有向无环图(允许重边,点用1到n的整数表示),每条边上有一个字符 ...

  8. 搭建团队协作办公wiki (confluence)

    搭建环境 操作系统:centos7 数据库:mysql 一.准备工作 下载软件:atlassian-confluence-6.7.1-x64.bin wget https://downloads.at ...

  9. springCloud——Dalston.SR5升级到Greenwich.SR2

    老项目: SpringBoot 版本 :1.5.13.RELEASE SpringCloud 版本:Dalston.SR5 项目升级: SpringBoot 版本 :2.1.6.RELEASE Spr ...

  10. 【shell】sed处理多行合并

    有这么一个题 文件格式 table=t1 name owner address table=t2 id text col1 comment col5 table=t3 prod_name price ...