国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

如何在Microsoft.NET中自定義配置文件

2019-11-18 12:05:38
字體:
供稿:網(wǎng)友

  摘要
  使用Microsoft.NET開發(fā)一個項目時,可能包含了Windows應(yīng)用程序、Web應(yīng)用程序、Web Service、Windows Service等多種應(yīng)用。假如您想使這幾個應(yīng)用程序使用同一個配置(比如同一個數(shù)據(jù)庫連接),而又不想重復編寫不同的配置文件。那么.NET提供的配置文件方案可能就不能達到你的目的了。本文介紹一種簡單的使用xml格式的配置文件及其使用方法。本文假設(shè)您的項目有至少一個Windows應(yīng)用程序、一個Web應(yīng)用程序和一個Window Service應(yīng)用。
  
  配置文件結(jié)構(gòu)
  為了使所有應(yīng)用程序均可訪問到該配置文件,本實例將配置文件放在WINNT/SYSTEM32系統(tǒng)目錄下。當然,讀者可以自己定義文件存放的位置,但需要注重程序的移植性。Windows系統(tǒng)目錄可以使用Windows API函數(shù)獲取,但要求使用的Windows、Web和Window Service應(yīng)用程序?qū)ο到y(tǒng)目錄有讀取的權(quán)限。
  
  為了方便闡述,我們將該配置文件命名為System.config(與微軟.NET的配置文件擴展名相同),在程序中假如不指定配置文件名,則配置文件默認為System.config。
  
  配置文件的結(jié)構(gòu)如下,讀者可以根據(jù)自己的需要對配置文件進行添刪。
  
  <?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節(jié)點上,另一種將配置值寫在節(jié)點的屬性上。下面的章節(jié)中將對這兩種節(jié)點配置的獲取和設(shè)置分別說明。
  
  配置文件采用xml結(jié)構(gòu),關(guān)于xml的語法和概念,網(wǎng)絡(luò)上的相關(guān)資料很多,請讀者自己參考網(wǎng)絡(luò)資源。第一個節(jié)點使用子節(jié)點保存數(shù)據(jù)庫配置,通過獲取子節(jié)點值來獲取數(shù)據(jù)庫的配置。第二個節(jié)點使用節(jié)點的屬性來保存數(shù)據(jù)庫配置。
  
  讀取配置
  下面將講述如何使用程序讀取System.config配置文件。
  
  1、輔助程序:下面的程序段使用Windows API函數(shù)獲取系統(tǒng)目錄。
  
  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函數(shù)GetSystemDirectory(StringBuilder,int)。最后重寫該方法,GetSystemDirectory()方法調(diào)用API函數(shù),將系統(tǒng)目錄作為字符串返回。
  
  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
   {
   //根據(jù)指定路徑獲取節(jié)點
   XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
  
   //獲取節(jié)點的屬性,并循環(huán)取出需要的屬性值
   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
   {
   //根據(jù)路徑獲取節(jié)點
   XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
   strReturn = xmlNode.InnerText;
   }
   catch(XmlException xmle)
   {
   System.Console.WriteLine(xmle.Message);
   }
   return strReturn;
   }
  
  這里我們先引用了System.Xml名稱空間,在構(gòu)造函數(shù)中,指定配置文件到系統(tǒng)目錄下的System.config。然后使用XmlDocument的Load()方法將該文件讀入XmlDocument對象xmlDoc。
  
  GetConfigValue(string strNode,string strAttribute)方法讀取指定節(jié)點的指定屬性值。如配置文件的節(jié)點的server屬性。
  
  GetConfigValue(string strNode)方法讀取指定節(jié)點的值,如第一節(jié)所述配置文件節(jié)點的子節(jié)點的值。
  
  治理配置
  下面的程序示例提供治理配置文件的三個主要方法
  
  public void SetConfigValue(string strNode,string newValue)
  {
   try
   {
   //根據(jù)指定路徑獲取節(jié)點
   XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
  
   //設(shè)置節(jié)點值
   xmlNode.InnerText = newValue;
   }
   catch(XmlException xmle)
   {
   throw xmle;
   }
   }
  
   public void SetConfigValue(string strNode,string strAttribute,string newValue)
   {
   try
   {
   //根據(jù)指定路徑獲取節(jié)點
   XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
  
   //獲取節(jié)點的屬性,并循環(huán)取出需要的屬性值
   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
   {
   //保存設(shè)置的結(jié)果
   xmlDoc.Save(strConfigFile);
   }
   catch(XmlException xmle)
   {
   throw xmle;
   }
   }
  
  SetConfigValue(string strNode,string newValue)用來設(shè)置節(jié)點值,SetConfigValue(string strNode,string strAttribute,string newValue)用來設(shè)置節(jié)點的屬性值。在修改了配置內(nèi)容后,必須調(diào)用SaveConnfig()方法,用來將修改過的配置保存到文件。
  
  總結(jié)
  配制文件有許多種形式,本文所提的只是一種自己編寫的配制文件。當然,對本文的程序做一點點修改,您可以編寫出其它各種各樣符合程序?qū)嶋H需要的配制文件。希望本文對您開發(fā)應(yīng)用程序有些幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 丰都县| 永吉县| 富阳市| 曲水县| 定南县| 绍兴县| 新巴尔虎左旗| 昂仁县| 新乡市| 鲜城| 顺昌县| 荃湾区| 鹿泉市| 台中县| 巴南区| 红原县| 大名县| 樟树市| 威宁| 栾川县| 隆林| 抚顺市| 青岛市| 民丰县| 石城县| 宿松县| 舞钢市| 长葛市| 东海县| 永定县| 黔东| 上林县| 安化县| 桃源县| 绥阳县| 仙游县| 阳朔县| 石门县| 太和县| 菏泽市| 防城港市|