服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

如何在microsoft.net中自定义配置文件


  摘要
  使用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的语法和概念,网络上的相关资料很多,请读者自己参考网络资源。第一个节点使用子节点保存数据库配置,通过获取子节点值来获取数据库的配置。第二个节点使用节点的属性来保存数据库配置。
  
  读取配置
  下面将讲述如何使用程序读取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)方法读取指定节点的指定属性值。如配置文件的节点的server属性。
  
  getconfigvalue(string strnode)方法读取指定节点的值,如第一节所述配置文件节点的子节点的值。
  
  管理配置
  下面的程序示例提供管理配置文件的三个主要方法
  
  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()方法,用来将修改过的配置保存到文件。
  
  总结
  配制文件有许多种形式,本文所提的只是一种自己编写的配制文件。当然,对本文的程序做一点点修改,您可以编写出其它各种各样符合程序实际需要的配制文件。希望本文对您开发应用程序有些帮助。

扫描关注微信公众号