如何使用.NET配置文件(二)
生活随笔
收集整理的這篇文章主要介紹了
如何使用.NET配置文件(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
.NET的應用程序配置文件,使用的是XML格式。相對INI文件來說,它的功能要強上不少,而且具有很強的可擴展性。它的缺點是不能直接進行寫操作,也就是說,不能直接在程序中修改配置文件的數據(當然不是指不能,不過不是本文討論的范圍)。本文主要目的是探討如何擴展配置文件,并在其加入各種自定義配置信息。
??<myInfo?Area="Fuzhou"?Device="Printer"?Customer="Muf"?/>
??<myInfo?Area="Shanghai"?Device="Mobile"?Customer="Liny"?/>
</myConfig>
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"]);
}
{
?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;
?}
}
<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>
如何使用.NET配置文件(一)??? 如何使用.NET配置文件(二)??? 沐楓網志
??? 3. 自定義配置結構 (使用IConfigurationSectionHandler)
?假設有以下的配置信息,其在MyInfo可以重復許多次,那么應如何讀取配置呢?這時就要使用自定義的配置程序了。
??<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來實現任意的配置文件結構。
轉載于:https://www.cnblogs.com/xiaobaidhg/archive/2007/03/20/681022.html
總結
以上是生活随笔為你收集整理的如何使用.NET配置文件(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 恢复误删数据(SQL Server 20
- 下一篇: 完成了WF工作流持久化和对持久化介质数据