博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# App.config 自定义 配置节
阅读量:4706 次
发布时间:2019-06-10

本文共 1398 字,大约阅读时间需要 4 分钟。

 

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

转载于:https://www.cnblogs.com/tianciliangen/p/5977289.html

你可能感兴趣的文章
查看手机已经记住的WIFI密码
查看>>
最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
查看>>
C# 两个datatable中的数据快速比较返回交集或差集
查看>>
关于oracle样例数据库emp、dept、salgrade的mysql脚本复杂查询分析
查看>>
adb shell am 的用法
查看>>
iOS10 UI教程视图和子视图的可见性
查看>>
FindChildControl与FindComponent
查看>>
中国城市json
查看>>
android下载手动下载Android SDK
查看>>
C++学习:任意合法状态下汉诺塔的移动(原创)
查看>>
leetcode133 - Clone Graph - medium
查看>>
UNET学习笔记2 - 高级API(HLAPI)
查看>>
"ORA-00942: 表或视图不存在 "的原因和解决方法[转]
查看>>
Oauth支持的5类 grant_type 及说明
查看>>
C#中用DateTime的ParseExact方法解析日期时间(excel中使用系统默认的日期格式)
查看>>
W3100SM-S 短信猫代码发送 上
查看>>
netty接收大文件的方法
查看>>
软件工程设计之四则运算
查看>>
SpringMVC @ResponseBody 406
查看>>
Partial Tree UVALive - 7190(完全背包)
查看>>