天天看點

.net應用程式配置檔案——自定義配置節

1.        配置檔案概述:

應用程式配置檔案是标準的 XML 檔案,XML 标記和屬性是區分大小寫的。它是可以按需要更改的,開發人員可以使用配置檔案來更改設定,而不必重編譯應用程式。配置檔案的根節點是configuration。我們經常通路的是appSettings,它是由.Net預定義配置節。我們經常使用的配置檔案的架構是象下面的形式。先大概有個印象,通過後面的執行個體會有一個比較清楚的認識。下面的“配置節”可以了解為進行配置一個XML的節點。

 常見配置檔案模式:

配置檔案概述:

應用程式配置檔案是标準的 XML 檔案,XML 标記和屬性是區分大小寫的。它是可以按需要更改的,開發人員可以使用配置檔案來更改設定,而不必重編譯應用程式。配置檔案的根節點是configuration。我們經常通路的是appSettings,它是由.Net預定義配置節。我們經常使用的配置檔案的架構是象下面的形式。先大概有個印象,通過後面的執行個體會有一個比較清楚的認識。下面的“配置節”可以了解為進行配置一個XML的節點。

 常見配置檔案模式:

<configuration>

        <configSections>    //配置節聲明區域,包含配置節和命名空間聲明

                <section>              //配置節聲明

             <sectionGroup>       //定義配置節組

                  <section>       //配置節組中的配置節聲明

        <appSettings> //預定義配置節

        <Custom element for configuration section>  //配置節設定區域

 2.        隻有appSettings節的配置檔案及通路方法

下面是一個最常見的應用程式配置檔案的例子,隻有appSettings節。

.net應用程式配置檔案——自定義配置節

<? xml version = " 1.0 "  encoding = " utf-8 " ?>

.net應用程式配置檔案——自定義配置節

< configuration >

.net應用程式配置檔案——自定義配置節

     < appSettings >

.net應用程式配置檔案——自定義配置節

         < add key = " connectionstring "  value = " User ID=sa;Data Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1; "   />

.net應用程式配置檔案——自定義配置節

         < add key = " TemplatePATH "  value = " Template "   />

.net應用程式配置檔案——自定義配置節

     </ appSettings >

.net應用程式配置檔案——自定義配置節

</ configuration >

.net應用程式配置檔案——自定義配置節

下面來看看這樣的配置檔案如何方法。

string _connectionString=ConfigurationSettings.AppSettings["connectionstring"];

使用ConfigurationSettings類的靜态屬性AppSettings就可以直接方法配置檔案中的配置資訊。這個屬性的類型是NameValueCollection。

  3.        自定義配置檔案

3.1 自定義配置節

一個使用者自定義的配置節,在配置檔案中分為兩部分:一是在<configSections></ configSections>配置節中聲明配置節(上面配置檔案模式中的“<section>”),另外是在<configSections></ configSections >之後設定配置節(上面配置檔案模式中的“<Custom element for configuration section>”),有點類似一個變量先聲明,後使用一樣。聲明一個配置檔案的語句如下:

自定義配置檔案

3.1 自定義配置節

一個使用者自定義的配置節,在配置檔案中分為兩部分:一是在<configSections></ configSections>配置節中聲明配置節(上面配置檔案模式中的“<section>”),另外是在<configSections></ configSections >之後設定配置節(上面配置檔案模式中的“<Custom element for configuration section>”),有點類似一個變量先聲明,後使用一樣。聲明一個配置檔案的語句如下:

  < section name =" " type =" "/>

<section>:聲明新配置節,即可建立新配置節。

name:自定義配置節的名稱。

type:自定義配置節的類型,主要包括System.Configuration.SingleTagSectionHandler、System.Configuration.DictionarySectionHandler、System.Configuration.NameValueSectionHandler。

不同的type不但設定配置節的方式不一樣,最後通路配置檔案的操作上也有差異。下面我們就舉一個配置檔案的例子,讓它包含這三個不同的type。

.net應用程式配置檔案——自定義配置節

<? xml version = " 1.0 "  encoding = " utf-8 "   ?>

.net應用程式配置檔案——自定義配置節

< configuration >

.net應用程式配置檔案——自定義配置節

     < configSections >

.net應用程式配置檔案——自定義配置節

         < section name = " Test1 "  type = " System.Configuration.SingleTagSectionHandler " />

.net應用程式配置檔案——自定義配置節

         < section name = " Test2 "  type = " System.Configuration.DictionarySectionHandler " />

.net應用程式配置檔案——自定義配置節

         < section name = " Test3 "  type = " System.Configuration.NameValueSectionHandler "   />

.net應用程式配置檔案——自定義配置節

     </ configSections >

.net應用程式配置檔案——自定義配置節
.net應用程式配置檔案——自定義配置節

     < Test1 setting1 = " Hello "  setting2 = " World " />

.net應用程式配置檔案——自定義配置節

     < Test2 >

.net應用程式配置檔案——自定義配置節

         < add key = " Hello "  value = " World "   />

.net應用程式配置檔案——自定義配置節

     </ Test2 >

.net應用程式配置檔案——自定義配置節

     < Test3 >

.net應用程式配置檔案——自定義配置節

         < add key = " Hello "  value = " World "   />

.net應用程式配置檔案——自定義配置節

     </ Test3 >

.net應用程式配置檔案——自定義配置節
.net應用程式配置檔案——自定義配置節

</ configuration >

.net應用程式配置檔案——自定義配置節

我們對上面的自定義配置節進行說明。在聲明部分使用 < section name ="Test1" type ="System.Configuration.SingleTagSectionHandler"/> 聲明了一個配置節它的名字叫Test1,類型為SingleTagSectionHandler。在設定配置節部分使用     <Test1 setting1="Hello" setting2="World"/>設定了一個配置節,它的第一個設定的值是Hello,第二個值是World,當然還可以有更多。其它的兩個配置節和這個類似。

下面我們看在程式中如何通路這些自定義的配置節。我們用過ConfigurationSettings類的靜态方法GetConfig來擷取自定義配置節的資訊。

public static object GetConfig(string sectionName);               
 
下面是通路這三個配置節的代碼:
       

下面是通路這三個配置節的代碼:

.net應用程式配置檔案——自定義配置節

             // 通路配置節Test1

.net應用程式配置檔案——自定義配置節

            IDictionary IDTest1  =  (IDictionary)ConfigurationSettings.GetConfig( " Test1 " );

.net應用程式配置檔案——自定義配置節

             string  str  =  ( string )IDTest1[ " setting1 " ]  + "   " + ( string )IDTest1[ " setting2 " ];

.net應用程式配置檔案——自定義配置節

            MessageBox.Show(str);         // 輸出Hello World

.net應用程式配置檔案——自定義配置節
.net應用程式配置檔案——自定義配置節

             // 通路配置節Test1的方法2

.net應用程式配置檔案——自定義配置節

             string [] values1 = new   string [IDTest1.Count];

.net應用程式配置檔案——自定義配置節

            IDTest1.Values.CopyTo(values1, 0 );

.net應用程式配置檔案——自定義配置節

            MessageBox.Show(values1[ 0 ] + "   " + values1[ 1 ]);     // 輸出Hello World

.net應用程式配置檔案——自定義配置節
.net應用程式配置檔案——自定義配置節

             // 通路配置節Test2

.net應用程式配置檔案——自定義配置節

            IDictionary IDTest2  =  (IDictionary)ConfigurationSettings.GetConfig( " Test2 " );

.net應用程式配置檔案——自定義配置節

             string [] keys = new   string [IDTest2.Keys.Count];

.net應用程式配置檔案——自定義配置節

             string [] values = new   string [IDTest2.Keys.Count];

.net應用程式配置檔案——自定義配置節

            IDTest2.Keys.CopyTo(keys, 0 );

.net應用程式配置檔案——自定義配置節

            IDTest2.Values.CopyTo(values, 0 );

.net應用程式配置檔案——自定義配置節

            MessageBox.Show(keys[ 0 ] + "   " + values[ 0 ]);

.net應用程式配置檔案——自定義配置節
.net應用程式配置檔案——自定義配置節

             // 通路配置節Test3

.net應用程式配置檔案——自定義配置節

            NameValueCollection nc = (NameValueCollection)ConfigurationSettings.GetConfig( " Test3 " );

.net應用程式配置檔案——自定義配置節

            MessageBox.Show(nc.AllKeys[ 0 ].ToString() + "   " + nc[ " Hello " ]);     // 輸出Hello World

.net應用程式配置檔案——自定義配置節

通過上面的代碼我們可以看出,不同的type通過GetConfig傳回的類型不同,具體獲得配置内容的方式也不一樣。

配置節處理程式 傳回類型
SingleTagSectionHandler Systems.Collections.IDictionary
DictionarySectionHandler Systems.Collections.IDictionary
NameValueSectionHandler Systems.Collections.Specialized.NameValueCollection

  3.2  自定義配置節組

配置節組是使用<sectionGroup>元素,将類似的配置節分到同一個組中。配置節組聲明部分将建立配置節的包含元素,在<configSections>元素中聲明配置節組,并将屬于該組的節置于<sectionGroup>元素中。下面是一個包含配置節組的配置檔案的例子:

.net應用程式配置檔案——自定義配置節

<? xml version = " 1.0 "  encoding = " utf-8 "   ?>

.net應用程式配置檔案——自定義配置節

< configuration >

.net應用程式配置檔案——自定義配置節

     < configSections >

.net應用程式配置檔案——自定義配置節

         < sectionGroup name = " TestGroup " >

.net應用程式配置檔案——自定義配置節

             < section name = " Test "  type = " System.Configuration.NameValueSectionHandler " />

.net應用程式配置檔案——自定義配置節

         </ sectionGroup >

.net應用程式配置檔案——自定義配置節

     </ configSections >

.net應用程式配置檔案——自定義配置節
.net應用程式配置檔案——自定義配置節

     < TestGroup >

.net應用程式配置檔案——自定義配置節

         < Test >

.net應用程式配置檔案——自定義配置節

             < add key = " Hello "  value = " World " />

.net應用程式配置檔案——自定義配置節

         </ Test >

.net應用程式配置檔案——自定義配置節

     </ TestGroup >

.net應用程式配置檔案——自定義配置節

</ configuration >

下面是通路這個配置節組的代碼:

NameValueCollection myvalue=(NameValueCollection)ConfigurationSettings.GetConfig("TestGroup/Test");

MessageBox.Show(myvalue.AllKeys[0].ToString()+""+myvalue["Hello"]); //輸出HelloWorld

4.自定義配置節處理函數

我們可以重用。net自帶的配置節應用程式,也可以定義自己的配置節應用程式。

自定義配置節處理程式必須實作IconfigurationSectionHandler接口,這個接口隻有一個名為Create的方法,該方法接受三個參數:一個名為parent的object變量,一個HttpConfigurationContext對象變量,一個名為section的XmlNode變量。

<!--web.config-->

<configuration>

    <configSections>

        <section name="mySection"

                 type="Custom.CustomTagHandler, Custom" />

    </configSections>

    <mySection>

        <AppName> MyApplication</AppName>

    </mySection>

... ...

以上聲明了一個mySection元素,并在configSections中聲明了該配置的處理程式類名為Custom.CustomTagHandler,Custom為程式集名稱。接下來我們可以通過一個實作System.Configuration.IConfigurationSectionHandler接口的類來處理該配置元素,如:

<!--CustomTagHandler.cs-->

using Custom.Data;

namespace Custom

{

  public class CustomTagHandler:IConfigurationSectionHandler

 {  //實作該接口的Create方法

  public object Create(object parent, object input, XmlNode node)

  {

   Data data = new Data();

   foreach(XmlNode xn in node.ChildNodes)

   {

    switch(xn.Name)

    {

     case("appName"):

      data.AppName = xn.InnerText;

      break;

     case("appVer"):

      data.AppVer = xn.InnerText;

      break;

      ... ...

    }//switch end

   }//foreach end

   return data;

  }//method end

 }

}

IConfigurationSectionHandler 接口隻有一種方法,每當發現注冊到處理程式的配置節時,都會在節處理程式上調用 Create 方法,我們實作的類傳回一個Data類的執行個體,該類是一個專門的資料集,代碼如下:

<!--Data.cs-->

namespace Custom.Data

{

 public class Data

 {

  public Data()

  {

  }

  public string AppName;//程式名稱

  public string AppVer;//程式版本

  public string AppAuthor;//程式作者

  ...  ...

 }

}

至此,現在可以讀取配置元素值了,如:

<!--Sample1.aspx-->

private void Page_Load(object sender, System.EventArgs e)

{

   Data data;

   data = ConfigurationSettings.GetConfig("mySection") as Data;

   this.tbName.Text = data.AppName;   

}