C# App.config 自定义 配置节
生活随笔
收集整理的這篇文章主要介紹了
C# App.config 自定义 配置节
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?方法一、
App.config <?xml?version="1.0"?encoding="utf-8"??><configuration>
??<configSections>
????<section?name="color"???type="System.Configuration.NameValueSectionHandler"?/>
????<section?name="message"?type="System.Configuration.DictionarySectionHandler"/>
????<section?name="name"???type="System.Configuration.SingleTagSectionHandler"/>
??</configSections>
??<color>
????<add?key="red"???value="#ff0000"/>
????<add?key="green"?value="#00ff00"/>
????<add?key="blue"??value="#0000ff"/>
??</color>
??<message>
????<add?key="welcome"?value="你好,歡迎"/>
??</message>
??<name?firstName="陳"?lastName="明明"/>
</configuration>?
對于自定義的配置節,應該先在 <configSections>中聲明要配置的節與類型
讀取自定義配置節 ?public?static?void?Main(string[]?args)????????{
????????????//get?color
????????????NameValueCollection?color?=?(NameValueCollection)ConfigurationManager.GetSection("color");
????????????foreach?(String?str?in?color.AllKeys)?{
????????????????Console.WriteLine(str+":"+color[str]);
????????????}
????????????//get?message
????????????IDictionary?message?=?(IDictionary)ConfigurationManager.GetSection("message");
????????????foreach?(String?str?in?message.Keys)?{
????????????????Console.WriteLine(str+":"+message[str]);
????????????}
????????????//?get?name
????????????IDictionary?name?=?(IDictionary)ConfigurationManager.GetSection("name");
????????????foreach?(String?str?in?name.Keys)
????????????{
????????????????Console.WriteLine(str?+?":"?+?name[str]);
????????????}???
????????????//Console.WriteLine(name["firstName"]);
????????????Console.Read();
????????}?
?方法二、通過ConfigurationSection【配置域】、ConfigurationElement【節點】、ConfigurationElementCollection【節點列表】實現自定義節
<configuration><configSections><section name="orders" type="ConsoleApplication4.OrdersSection, ConsoleApplication4"/></configSections> <orders companyID="2001"><order number="100001" amount="222.22"></order><order number="300001" amount="33.33"></order></orders> </configuration> App.config下面我們要定義相應的實體對象,該實體對象中會有一個子對象【用來表示節點列表信息】(ConfigurationElementCollection)
namespace ConsoleApplication4 {public class OrdersSection : ConfigurationSection{[ConfigurationProperty("companyID", IsRequired = true)]public string CompanyID{get{return (string)base["companyID"];}set{base["companyID"] = value;}}[ConfigurationProperty("", IsDefaultCollection = true)]public OrderElementCollection Orders{get{return (OrderElementCollection)base[""];}}}public class OrderElementCollection : ConfigurationElementCollection{protected override ConfigurationElement CreateNewElement(){return new OrderElement();}protected override object GetElementKey(ConfigurationElement element){return ((OrderElement)element).Number;}public override ConfigurationElementCollectionType CollectionType{get{return ConfigurationElementCollectionType.BasicMap;}}protected override string ElementName{get{return "order";}}public OrderElement this[int index]{get{return (OrderElement)BaseGet(index);}set{if (BaseGet(index) != null){BaseRemoveAt(index);}BaseAdd(index, value);}}}public class OrderElement : ConfigurationElement{[ConfigurationProperty("number", IsRequired = true)]public string Number{get{return (string)base["number"];}set{base["number"] = value;}}[ConfigurationProperty("amount", IsRequired = true)]public double Amount{get{return (double)base["amount"];}set{base["amount"] = value;}}} } 實現代碼 OrdersSection config = (OrdersSection)ConfigurationManager.GetSection("orders");Console.WriteLine("CompanyId={0}",config.CompanyID);for (int i = 0; i < config.Orders.Count; i++){Console.WriteLine("Amount={0},Number={1}", config.Orders[i].Amount, config.Orders[i].Number);} 讀取自定義節?
?
?
?
轉載于:https://www.cnblogs.com/S-TGM/archive/2011/09/07/2170386.html
總結
以上是生活随笔為你收集整理的C# App.config 自定义 配置节的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 针对Chrome的css的hack写法
- 下一篇: LINQ学习之旅——第二站LTQ之标准数