qingqing12 2004-12-7 23:56
[转帖]在.NET中自定义配置文件
目录<B></B>摘要
<B></B>配置文件结构
<B></B>读取配置
<B></B>管理配置
<B></B>总结摘要使用Microsoft.NET开发一个项目时,可能包含了Windows应用程序、Web应用程序、Web Service、Windows Service等多种应用。如果您想使这几个应用程序使用同一个配置(比如同一个数据库连接),而又不想重复编写不同的配置文件。那么.NET提供的配置文件方案可能就不能达到你的目的了。本文介绍一种简单的使用xml格式的配置文件及其使用方法。本文假设您的项目有至少一个Windows应用程序、一个Web应用程序和一个Window Service应用。配置文件结构为了使所有应用程序均可访问到该配置文件,本实例将配置文件放在WINNT\SYSTEM32系统目录下。当然,读者可以自己定义文件存放的位置,但需要注意程序的移植性。Windows系统目录可以使用Windows API函数获取,但要求使用的Windows、Web和Window Service应用程序对系统目录有读取的权限。为了方便阐述,我们将该配置文件命名为System.config(与微软.NET的配置文件扩展名相同),在程序中如果不指定配置文件名,则配置文件默认为System.config。配置文件的结构如下,读者可以根据自己的需要对配置文件进行添删。<?xml version="1.0" encoding="utf-8"?><root><!--Sql Server DB1--><systemdb><server>localhost</server><uid>sa</uid><pwd> </pwd><database>Pubs</database><pooling>True</pooling><maxpoolsize>20</maxpoolsize><minpoolsize>3</minpoolsize><lifetime>300</lifetime></systemdb><!--Sql Server DB2--><webdb server="localhost"uid="sa"pwd=""database="NorthWind"pooling="True"maxpoolsize="20"minpoolsize="3"lifetime="300"/><!—SMTP Server--><smtpserver server="pop3.microsoft.com" port="25" /></root>说明:可以看到,配置有两种形式,第一种的配置值直接写在xml节点上,另一种将配置值写在节点的属性上。下面的章节中将对这两种节点配置的获取和设置分别说明。配置文件采用xml结构,关于xml的语法和概念,网络上的相关资料很多,请读者自己参考网络资源。第一个节点<systemdb>使用子节点保存数据库配置,通过获取子节点值来获取数据库的配置。第二个节点<webdb>使用节点的属性来保存数据库配置。读取配置下面将讲述如何使用程序读取System.config配置文件。1、辅助程序:下面的程序段使用Windows API函数获取系统目录。using System.Runtime.InteropServices;
using System.Text;
[DllImport("kernel32")]
private static extern void GetSystemDirectory(StringBuilder SysDir,int count);
public string GetSystemDirectory()
{
const int nChars = 128;
StringBuilder Buff = new StringBuilder(nChars);
GetSystemDirectory(Buff,nChars);
return Buff.ToString();
}
这里我们先引用了System.Runtime.InteropServices名称空间,然后引用API函数GetSystemDirectory(StringBuilder,int)。最后重写该方法,GetSystemDirectory()方法调用API函数,将系统目录作为字符串返回。2、解析xml文件:本例使用XML DOM(Document Object Modal)类来解析xml文件。程序片断如下:using System.Xml;
private XmlDocument xmlDoc = new XmlDocument();
private string strConfigFile;
public SystemSetting()
{
strConfigFile = GetSystemDirectory() + @"\System.config";
xmlDoc.Load(strConfigFile);
}
public string GetConfigValue(string strNode,string strAttribute)
{
string strReturn = "";
try
{
//根据指定路径获取节点
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
//获取节点的属性,并循环取出需要的属性值
XmlAttributeCollection xmlAttr = xmlNode.Attributes;
for(int i=0 ;i<xmlAttr.Count; i++)
{
if (xmlAttr.Item(i).Name == strAttribute)
strReturn = xmlAttr.Item(i).Value;
}
}
catch(XmlException xmle)
{
throw xmle;
}
return strReturn;
}
public string GetConfigValue(string strNode)
{
string strReturn = "";
try
{
//根据路径获取节点
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
strReturn = xmlNode.InnerText;
}
catch(XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}
return strReturn;
}
这里我们先引用了System.Xml名称空间,在构造函数中,指定配置文件到系统目录下的System.config。然后使用XmlDocument的Load()方法将该文件读入XmlDocument对象xmlDoc。GetConfigValue(string strNode,string strAttribute)方法读取指定节点的指定属性值。如配置文件的<webdb>节点的server属性。GetConfigValue(string strN