C# App.config 自定义 配置节
2009-03-22 19:37
471 查看
(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();
}
<?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();
}
相关文章推荐
- [转]C#自定义应用程序配置文件App.config
- 在C#类库中使用App.config文件自定义配置
- C# App.config 自定义 配置节 出现的问题:配置系统未能初始化
- C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法
- 在C#类库中使用App.config文件自定义配置
- C# App.config 自定义 配置节
- [转]C#中使用自定义配置(app.config)
- C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法
- 自定义应用程序配置文件(app.config)_c#应用
- 在web.config和app.config文件中增加自定义配置节点的方法
- C# 读取保存App.config配置文件的完整源码参考
- 自定义应用程序配置文件(app.config)
- 在web.config和app.config文件中增加自定义配置节点
- C#实现把用户第一次选择的路径保存到配置文件app.config中
- 自定义应用程序配置文件(app.config)
- VS web.config/app.conifg配置文件自定义类型使用智能感知功能
- C#读取app.config数据库配置字符串
- C# 操作App.config 配置文件帮助类
- C# 读取保存App.config配置文件的完整源码参考
- 在web.config和app.config文件中增加自定义配置节点