日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# App.config 自定义 配置节

發(fā)布時間:2025/4/16 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# App.config 自定义 配置节 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?方法一、

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>?

對于自定義的配置節(jié),應該先在 <configSections>中聲明要配置的節(jié)與類型

讀取自定義配置節(jié) ?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【節(jié)點】、ConfigurationElementCollection【節(jié)點列表】實現(xiàn)自定義節(jié)

<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

下面我們要定義相應的實體對象,該實體對象中會有一個子對象【用來表示節(jié)點列表信息】(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;}}} } 實現(xiàn)代碼 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);} 讀取自定義節(jié)

?

?

?

?

轉載于:https://www.cnblogs.com/S-TGM/archive/2011/09/07/2170386.html

總結

以上是生活随笔為你收集整理的C# App.config 自定义 配置节的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。