.NET的應用程序配置文件,使用的是XML格式。相對INI文件來說,它的功能要強上不少,而且具有很強的可擴展性。它的缺點是不能直接進行寫操作,也就是說,不能直接在程序中修改配置文件的數據(當然不是指不能,不過不是本文討論的範圍)。本文主要目的是探討如何擴展配置文件,並在其加入各種自定義配置信息。
1. 使用<appSettings>
簡單的配置信息,可以直接放入<appSettings>標記中。如:
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="LogFile" value="d:/log/debug.log"/>
</appSettings>
</configuration>
相應訪問代碼如下:
string fileName = System.Configuration.ConfigurationSettings.AppSettings.Get("LogFile");
2. 自定義配置節(section)名稱
比如,我們要使用下面的配置結構,將配置信息歸類分組:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- 需要在此處加入自定義配置聲明 -->
<!-- 以下是自定義配置的內容 -->
<myConfig>
<myDictionary>
<add key="Area" value="Fuzhou"/>
<add key="Device" value="Printer"/>
<add key="Customer" value="Muf"/>
</myDictionary>
<myNameValue>
<add key="Area" value="Fuzhou"/>
<add key="Device" value="Printer"/>
<add key="Customer" value="Muf"/>
</myNameValue>
<myInfo
Area="Fuzhou" Device="Printer" Customer="Muf"
/>
</myConfig>
</configuration>
但是這樣是不行的。我們必須要在配置文件前面加入聲明:
<!-- 以下是自定義配置的聲明 -->
<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" />
</sectionGroup>
</configSections>
把這一段放入配置文件中,我們的配置結構就可以正常使用了。聲明中,<sectionGroup>用來定義不含配置數據的節的名稱。<section>用來定義含有自定義配置數據的節的名稱。<section type>用來指定定義配置數據的類型。.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" />
</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):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- 以下是自定義配置的聲明 -->
<configSections>
<section name="myConfig" type="myNamespace.MyInfoSectionHandler, myApp" />
</configSections>
<myConfigs>
<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
</myConfig>
</configuration>
根據上面的例子,我們可以使用IConfigurationSectionHandler來實現任意的配置文件結構。