按照MSDN描述?System.Configuration 命名空間 包含處理配置信息的類型
本篇文章主要兩方面的內容
1. 如何使用ConfigurationManager 讀取AppSetting和ConnectionStrings
2. 如何使用自定義 Section,我這里的自定義Section格式為
< SectionName > < services > 服務1的描述信息,供IoC容器使用 服務2的描述信息,供IoC容器使用 。。。 </ services > </ SectionName >
?
其實如果讀者使用一些比較出名的IoC 框架(Unity)都會有配置管理,根本不用自定義Section,但是技多不壓身,多了解點東西總是沒錯的。
?
一、ConfigurationManager 類
提供對客戶端應用程序配置文件的訪問。無法繼承此類
命名空間: System.Configuration
程序集: System.Configuration.dll
繼承關系:
原型定義:public static class ConfigurationManager
由上面的定義可以看出ConfigurationManager是一個靜態類
?
靜態屬性:
1. ConfigurationManager.AppSettings ? ? 獲取當前應用程序默認配置的 AppSettingsSection 數據
原型定義:public static NameValueCollection AppSettings { get; }
從上面的定義可以看出 AppSetting 是一個 鍵值對
?
2. ConfigurationManager.ConnectionStrings ?獲取當前應用程序默認配置的 ConnectionStringsSection 數據
例子:
app.config

1 <? xml version="1.0" ?> 2 < configuration > 3 < startup > 4 < supportedRuntime version ="v2.0.50727" /> 5 </ startup > 6 7 < connectionStrings > 8 < add name ="KKSEntities" connectionString ="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=XXX.XXX.XXX.XXX;initial catalog=KKS;user id=XXXX;password=KKKKK;multipleactiveresultsets=True;App=EntityFramework"" providerName ="System.Data.EntityClient" /> 9 </ connectionStrings > 10 11 < appSettings > 12 < add key ="Aphasia" value ="www.cnblogs.com/Aphasia" /> 13 </ appSettings > 14 </ configuration >
Program.cs

1 using System; 2 using System.Configuration; 3 4 namespace ConsoleApplication1 5 { 6 class Program 7 { 8 static void Main( string [] args) 9 { 10 string web = ConfigurationManager.AppSettings[ " Aphasia " ]; 11 Console.WriteLine(web); 12 13 string cnstr = ConfigurationManager.ConnectionStrings[ " KKSEntities " ].ConnectionString; 14 Console.WriteLine(cnstr); 15 16 } 17 } 18 }
效果:
?
二、 自定義Section
app.config

1 <? xml version="1.0" ?> 2 < configuration > 3 < configSections > 4 < section name ="ServicesSection" type ="CustomDemo.ServicesSection,CustomDemo" /> 5 </ configSections > 6 7 < ServicesSection > 8 < services > 9 < add ServiceName ="XXX服務1" Impl ="XXX.Impl.XXXService1" Inter ="XXX.Inter.XXXInter1" /> 10 < add ServiceName ="XXX服務2" Impl ="XXX.Impl.XXXService2" Inter ="XXX.Inter.XXXInter2" /> 11 < add ServiceName ="XXX服務3" Impl ="XXX.Impl.XXXService3" Inter ="XXX.Inter.XXXInter3" /> 12 </ services > 13 </ ServicesSection > 14 15 < startup > 16 < supportedRuntime version ="v2.0.50727" /> 17 </ startup > 18 </ configuration >
Program.cs

1 using System; 2 using System.Configuration; 3 4 namespace CustomDemo 5 { 6 class Program 7 { 8 static void Main( string [] args) 9 { 10 ServicesSection myServices = ConfigurationManager.GetSection( " ServicesSection " ) as ServicesSection; 11 foreach (Service item in myServices.ServiceItems) 12 { 13 Console.WriteLine( " ServiceName:{0} Impl:{1} Inter:{2} " , item.ServiceName, item.Impl, item.Inter); 14 } 15 } 16 } 17 18 #region [自定義Section處理代碼] 19 public class Service : ConfigurationElement 20 { 21 #region [當遇不能識別的元素時不讓程序報錯] 22 protected override bool OnDeserializeUnrecognizedAttribute( string name, string value) 23 { 24 return true ; 25 } 26 protected override bool OnDeserializeUnrecognizedElement( string elementName, System.Xml.XmlReader reader) 27 { 28 return true ; 29 } 30 #endregion 31 32 #region [節點元素] 33 [ConfigurationProperty( " ServiceName " , IsRequired = true )] 34 public string ServiceName 35 { 36 get { return this [ " ServiceName " ].ToString(); } 37 } 38 39 [ConfigurationProperty( " Impl " , IsRequired = true )] 40 public string Impl 41 { 42 get { return this [ " Impl " ].ToString(); } 43 } 44 45 [ConfigurationProperty( " Inter " , IsRequired = true )] 46 public string Inter 47 { 48 get { return this [ " Inter " ].ToString(); } 49 } 50 #endregion 51 } 52 53 public class Services : ConfigurationElementCollection 54 { 55 protected override ConfigurationElement CreateNewElement() 56 { 57 return new Service(); 58 } 59 protected override object GetElementKey(ConfigurationElement element) 60 { 61 return ((Service)element).ServiceName; 62 } 63 } 64 65 public class ServicesSection : ConfigurationSection 66 { 67 [ConfigurationProperty( " services " , IsDefaultCollection = false )] 68 public Services ServiceItems { get { return (Services) base [ " services " ]; } } 69 } 70 #endregion 71 }
效果:
本小節代碼 下載
?
三、參考資料
1.? .net自定義configSections的5個示例
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
