在.NET中自定义配置文件(转)

来源:互联网 发布:淘宝天下传媒面试 编辑:程序博客网 时间:2024/04/30 04:55
 

目录
  • 摘要
  • 配置文件结构
  • 读取配置
  • 管理配置
  • 总结

摘要
使用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 strNode)方法读取指定节点的值,如第一节所述配置文件<systemdb>节点的<server>子节点的值。

管理配置
下面的程序示例提供管理配置文件的三个主要方法
public void SetConfigValue(string strNode,string newValue){try{//根据指定路径获取节点XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);//设置节点值xmlNode.InnerText = newValue;}catch(XmlException xmle){throw xmle;}}public void SetConfigValue(string strNode,string strAttribute,string newValue){try{//根据指定路径获取节点XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);//获取节点的属性,并循环取出需要的属性值XmlAttributeCollection xmlAttr = xmlNode.Attributes;for(int i=0 ;i<xmlAttr.Count; i++){if (xmlAttr.Item(i).Name == strAttribute)xmlAttr.Item(i).Value = newValue;}}catch(XmlException xmle){throw xmle;}}public void SaveConfig(){try{//保存设置的结果xmlDoc.Save(strConfigFile);}catch(XmlException xmle){throw xmle;}}
SetConfigValue(string strNode,string newValue)用来设置节点值,SetConfigValue(string strNode,string strAttribute,string newValue)用来设置节点的属性值。在修改了配置内容后,必须调用SaveConnfig()方法,用来将修改过的配置保存到文件。

总结
配制文件有许多种形式,本文所提的只是一种自己编写的配制文件。当然,对本文的程序做一点点修改,您可以编写出其它各种各样符合程序实际需要的配制文件。希望本文对您开发应用程序有些帮助。

作者:潘兆勇