.net 配置文件读取方法(app.config&web.config)
时间:2010-10-04 来源:-FlySky-
先来看下配置文件的内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="company-config" type="FlySky.Blog.Configuration.ConfigurtionHandler.CompanyConfigHandler,FlySky.Blog.Configuration"/>
<section name="company-section" type="FlySky.Blog.Configuration.ConfigurationSection.CompanyInfo,FlySky.Blog.Configuration"/>
</configSections>
<company-config>
<dept name="dept1" count="10">
<members>
<member name="flysky1" age="27"></member>
<member name="flysky2" age="27"></member>
</members>
</dept>
<dept name="dept2" count="20"></dept>
</company-config>
<company-section name="飞航科技">
<depts>
<add name="技术部"></add>
<add name="人事部"></add>
</depts>
</company-section>
<appSettings>
<add key="test1" value="test1" />
<add key="test2" value="test2" />
</appSettings>
<connectionStrings>
<add name="db1" connectionString="database=flysky;uid=sa;pwd=flysky;server=."/>
<add name="db2" connectionString="database=flysky;uid=sa;pwd=flysky;server=."/>
</connectionStrings>
</configuration>
一、系统预置配置的读取
其中图中1为系统预置的配置地方读取方法如下比较简单:
1、首先引入:using System.Configuration;
2、
string test1 = System.Configuration.ConfigurationManager.AppSettings["test1"];
string test2 = System.Configuration.ConfigurationManager.AppSettings["test2"];
string db1 = System.Configuration.ConfigurationManager.ConnectionStrings["db1"].ConnectionString;
string db2 = System.Configuration.ConfigurationManager.ConnectionStrings["db2"].ConnectionString;
二、自定义配置的读取
自定义读取有2种方法。第一种通过实现IConfigurationSectionHandler接口来实现 ,第二种通过继承ConfigurationSection来实现
先来看第一种:
首先要添加<section name="company-config" type="FlySky.Blog.Configuration.ConfigurtionHandler.CompanyConfigHandler,FlySky.Blog.Configuration"/> 这个配置
CompanyConfigHandler的实现代码如下:
public class CompanyConfigHandler:IConfigurationSectionHandler
{
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
Company company = new Company();
var nodeList = section.ChildNodes;
List<Dept> depts = new List<Dept>();
foreach (XmlNode node in nodeList)
{
Dept dept = new Dept();
dept.Name = node.Attributes["name"].Value;
dept.Count = int.Parse(node.Attributes["count"].Value);
List<Member> members = new List<Member>();
foreach (XmlNode parentEx in node.ChildNodes)
{
foreach (XmlNode child in parentEx.ChildNodes)
{
Member member = new Member();
member.Name = child.Attributes["name"].Value;
member.Age = int.Parse(child.Attributes["age"].Value);
members.Add(member);
}
}
dept.Members = members;
depts.Add(dept);
}
company.Depts = depts;
return company;
}
}
其中用到几个辅助的对象用来对应配置的内容代码如下:
public class Company
{
public IList<Dept> Depts { get; set; }
public static Company GetCompany()
{
return System.Configuration.ConfigurationManager.GetSection("company-config") as Company;
}
}
public class Dept
{
public string Name { get; set; }
public int Count { get; set; }
public IList<Member> Members { get; set; }
}
public class Member
{
public string Name { get; set; }
public int Age { get; set; }
}
获取配置信息调用:GetCompany方法即可返回一个Company 类型的数据对象。
第二种通过继承ConfigurationSection来实现,还需要自定义的对象继承ConfigurationElementCollection与ConfigurationElement来完成
代码如下:
public class CompanyInfo : System.Configuration.ConfigurationSection
{
public static CompanyInfo GetCompanyInfo()
{
return (CompanyInfo)System.Configuration.ConfigurationManager.GetSection("company-section");
}
[ConfigurationProperty("name", IsRequired = false)]
public string Name
{
get
{
return this["name"].ToString();
}
}
[ConfigurationProperty("depts")]
public DeptCollection Depts
{
get
{
return this["depts"] as DeptCollection;
}
}
}
public class DeptCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new DeptInfo();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((DeptInfo)element).Name;
}
}
public class DeptInfo : ConfigurationElement
{
[ConfigurationProperty("name")]
public string Name
{
get
{
return this["name"].ToString();
}
}
}
由于比较简单代码不做解释。
本人推荐第二种方法实现IConfigurationSectionHandler接口来做,比较灵活,通过继承ConfigurationSection这个的方法比较简单但是
ConfigurationElementCollection里面的配置好像必须为<add />这样的配置有的时候不太友好。











