NET的應用程序配置文件
使用的是XML格式
相對INI文件來說
它的功能要強上不少
而且具有很強的可擴展性
它的缺點是不能直接進行寫操作
也就是說
不能直接在程序中修改配置文件的數據(當然不是指不能
不過不是本文討論的范圍)
本文主要目的是探討如何擴展配置文件
並在其加入各種自定義配置信息
使用
簡單的配置信息
可以直接放入標記中
如

<xml version=
encoding=
utf
?>

<appSettings>

<add key=
LogFile
value=
d:\log\debug
log
/>

appSettings>

< SPAN>configuration>
相應訪問代碼如下:

string fileName = System.Configuration.ConfigurationSettings.AppSettings.Get("LogFile");
2. 自定義配置節(section)名稱
比如,我們要使用下面的配置結構,將配置信息歸類分組:


<configuration>

<myConfig>

<myDictionary>

<add key="Area" value="Fuzhou"/>

<add key="Device" value="Printer"/>

<add key="Customer" value="Muf"/>

< SPAN>myDictionary>

<myNameValue>

<add key="Area" value="Fuzhou"/>

<add key="Device" value="Printer"/>

<add key="Customer" value="Muf"/>

< SPAN>myNameValue>

<myInfo

Area="Fuzhou" Device="Printer" Customer="Muf"

/>

< SPAN>myConfig>

< SPAN>configuration>
但是光這樣子說明是不行的。TW.WINGWIT.cOm沒有聲明,是不能使用自定義的配置段。我們必須要在配置文件前面加入聲明:

<configSections>

<sectionGroup name="myConfig">

<section name="myDictionary"

type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

<section name="myNameValue"

type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

<section name="myInfo"

type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

< SPAN>sectionGroup>

< SPAN>configSections>
聲明和配置的關系,示意圖如下:
由圖上可以看出,NameValueSectionHandler和DictionarySectionHandler在定義配置文件的內容形式上是一樣的,都是用來設置內容的。只是返回到C#中的類不太一樣,可以參考下面的代碼示例。
另外,如果不關心Handler類的版本等信息,可以直接省略。如NameValueSectionHandler可以直接如下聲明:

<section name="myDictionary" type="System.Configuration.NameValueSectionHandler, System" />
把上面的聲明段放入配置文件中,我們的配置結構就可以正常使用了。聲明中,用來定義不含配置數據的節的名稱。用來定義含有自定義配置數據的節的名稱。用來指定定義配置數據的類型。.NET已經定義了3種配置類型:
a. NameValueSectionHandler
相應訪問代碼如下:

NameValueCollection myNameValue= (NameValueCollection)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myNameValue");

string Area = myNameValue["Area"];

string Device= myNameValue["Device"];

string Customer = myNameValue["Customer "];
b. DictionarySectionHandler
相應訪問代碼如下:

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myDictionary");

string Area = myNameValue["Area"];

string Device= myNameValue["Device"];

string Customer = myNameValue["Customer "];
c. SingleTagSectionHandler
相應訪問代碼如下:

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myInfo");

string Area = myNameValue["Area"];

string Device= myNameValue["Device"];

string Customer = myNameValue["Customer "];
這三種類型的詳細信息,可以參考 MSDN 文檔。同時.NET 還定義了IgnoreSectionHandler類型,為 System.Configuration 之外的系統所讀取和處理的配置節提供節處理程序定義。
除此之外,.NET提供了IConfigurationSectionHandler接口,這樣我們還可以自行進行擴展,以設計出我們自已的配置形式。
3. 自定義配置結構 (使用IConfigurationSectionHandler)
假設有以下的配置信息,其在MyInfo可以重復許多次,那麼應如何讀取配置呢?這時就要使用自定義的配置程序了。

<myConfigs>

<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />

<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />

< SPAN>myConfig>
訪問代碼如下:

Hashtable cfgTable = (Hashtable)ConfigurationSettings.GetConfig( "myConfigs" );


Debug.Assert( cfgTable.Count == 2);

Hashtable cfgFuzhou = (Hashtable)cfgTable["Fuzhou"];

Hashtable cfgShanghai = (Hashtable)cfgTable["Shanghai"];

Debug.Assert( cfgFuzhou["Device"] == "Printer" );

Debug.Assert( cfgShanghai["Device"] == "Mobile" );

Debug.Assert( cfgFuzhou["Customer"] == "Muf" );

Debug.Assert( cfgShanghai["Customer"] == "Liny" );


foreach(Hashtable cfg in cfgTable.Values)



{

Console.WriteLine("Area={0} Device={1} Customer={2}", cfg["Area"], cfg["Device"], cfg["Customer"]);

}

為了能使用上面的訪問代碼來訪問配置結構,我們需要生成一個特定的配置讀取類(ConfigurationSectionHandler),例子很簡單,就不多做說明了:

public class MyInfoSectionHandler: IConfigurationSectionHandler



{

public object Create(object parent, object configContext, System.Xml.XmlNode section)


{

Hashtable config = new Hashtable();

foreach(XmlNode node in section.ChildNodes)


{

if(node.Name != "myInfo")

throw new System.Configuration.ConfigurationException("不可識別的配置項", node);


Hashtable item = new Hashtable();

foreach(XmlAttribute attr in node.Attributes)


{

switch(attr.Name)


{

case "Area":

case "Device":

case "Customer":

item.Add(attr.Name, attr.Value);

break;

default:

throw new System.Configuration.ConfigurationException("不可識別的配置屬性", attr);

}

}

config.Add(item["Area"], item);

}

return config;

}

}

然後,我們再定義配置說明。其中,myNamespace.MyInfoSectionHandler 是MyInfoSectionHandler類的帶名字空間的完整名稱;myApp 則是定義MyInfoSectionHandler類的程序集不帶擴展名的名字(如myApp.dll或myApp.exe):


<configuration>

<configSections>

<section name="myConfig" type="myNamespace.MyInfoSectionHandler, myApp" />

< SPAN>configSections>

<myConfigs>

<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />

<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />

< SPAN>myConfig>

< SPAN>configuration>
根據上面的例子,我們可以使用IConfigurationSectionHandler來實現任意的配置文件結構。
From:http://tw.wingwit.com/Article/program/net/201311/11876.html