System.Configuraion概述:
System.Configuration命名空间包括处理配置文件的类,如操作配置节的类、获取数据库连接字符串的类和提供数据验证的类等。
使用System.Configuration命名空间,开发人员可以获取配置文件的管理类“ConfigurationManager”,并用其完成动态处理配置文件的相关操作。在网站中,配置文件的名字默认为“Web.Config”。在项目中,配置文件的名字默认为“App.Config”。
System.Configuration命名空间的类组成
配置文件保存的是运行玩站和程序的一些基础配置,System.Configuration命名空间内的类负责动态管理这些配置。下面为管理配置常用的类及其说明。
配置文件信息:Configuration类
Configuration类是读取配置文件的主要操作类,它是和Web.Confing和App.Config配置文件关联的类。通过次类,可以动态地修改这些配置文件。使用配置类需要先了解几个配置文件的术语:配置元素、配置属性和配置节。一下代码具备这个成员。
<configSections>
<section name="BookVersion"></sectioin>
</configSections>
其中“configSections”是配置节,“section”是配置元素,“name”是配置属性。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="student" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<student>
<add key="name" value="http://www.cnblogs.com/i80386/archive/2011/10/27/amily"/>
<add key="age" value="http://www.cnblogs.com/i80386/archive/2011/10/27/15"/>
<add key="sex" value="http://www.cnblogs.com/i80386/archive/2011/10/27/female"/>
</student>
</configuration>
注意事项:configSections 相当于定义变量,必须放在configuration
IDictionary istudent = (IDictionary)ConfigurationManager.GetSection("student");
string[] keys=new string[istudent.Keys.Count];
string[] values = new string[istudent.Keys.Count];
istudent.Keys.CopyTo(keys,0);
istudent.Values.CopyTo(values, 0);
foreach (var key in keys)
{
Console.WriteLine(key);
}
foreach (var value in values)
{
Console.WriteLine(value);
}
foreach (var key in istudent.Keys)
{
Console.WriteLine("{0}:{1}", key, istudent[key]);
}
本文介绍System.Configuration命名空间的应用,包括如何使用ConfigurationManager类动态处理配置文件,以及Configuration类的基本使用方法和注意事项。

8954

被折叠的 条评论
为什么被折叠?



