C# App.config 自定义 配置节
2016-10-19 14:46
393 查看
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
<?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)
- 自定义应用程序配置文件(app.config)_c#应用
- 在C#类库中使用App.config文件自定义配置
- [转]C#自定义应用程序配置文件App.config
- 在C#类库中使用App.config文件自定义配置
- C# App.config 自定义 配置节 出现的问题:配置系统未能初始化
- C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法
- C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法
- C# App.config 自定义 配置节
- 自定义应用程序配置文件(app.config)
- C#_App.config配置文件的读取与更新
- [导入].NET 2.0中,配置文件app.config的读写(VS2005,C#)
- .NET 2.0中,配置文件app.config的读写(VS2005,C#)
- 读自定义应用程序配置文件1(app.config)
- 自定义应用程序配置文件(app.config)
- 自定义应用程序配置文件(app.config)
- 自定义应用程序配置文件(app.config)
- 自定义应用程序配置文件(app.config) (引用)
- 自定义应用程序配置文件(app.config)
- 如何在web.config 或者 app.config 文件中配置自定义节点