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