今天 是第一次有這個想法,想和大家分享自己的編程經驗。我工作年齡那么短,能有資格寫好嗎?我只能說我盡力吧。這個系列講的一些“哇!原來還可以這樣寫”的知識。很多技巧也是自己折騰出來的。
我們C#實驗室,一直堅持沒事瞎折騰,一幫大老爺們,70后,80后,90后組成的一個50人群體,曾經我許諾過群友,6月1日后就好好搞,其實里面技術比我高的人數不勝數。很感激他們,我們人少,但是團結,有負責感情,職業方面的10年經驗的jack兄弟,有懂算法豐富經驗的Yamat兄弟(二哥),有WPF xaml方面的藍言, 藍言經常熱心幫助朋友解決問題,經常幫助基友遠程操作,比如藍言教yamat 面向對象編程,yamat也幫藍言和其他朋友,我們互相幫助學習提升友誼,還有賣萌搞笑技術也很好的Sandy(我們喊他三弟),還有很多好朋友,感謝他們給我幫助。
版權聲明:http://www.cnblogs.com/AaronYang/archive/2013/06/07/3123157.html 本文禁止任何轉載,保留AaronYang的知識產權。
1 . App.config文件中的細節(DEMO下載)
新建控制台程序,名字叫 TestConfig,然后新建文件夾 Config,修改目標框架,引用System.Configuration類庫
平時大家肯定都是把所有的配置都放到了app.config中去了,各別第三方的類庫使用的時候可能也要配置,一大堆冗余的配置一定不好管理。其實config中的配置是可以分離出去的,現在我們把app.config文件中的配置 分離到多個config文件中去,這些文件我們放到Config文件夾去,方便以后維護
1.1 configSource
在Config文件中新建DBConnection.config文件
添加配置
<?xml version="1.0" encoding="utf-8" ?><connectionStrings><add name="TestDBConnectionString" connectionString="data source=.;initial catalog=TestDB;user id=sa;password=sa"/></connectionStrings>
右鍵DBConnection.config文件設置一下生成屬性:
選擇 如果教新則復制,這樣生成后 Debug或者Release文件夾下才會有這個文件,我們才能讀取配置
打開app.config 添加配置如下:
<?xml version="1.0"?>
<configuration><connectionStrings configSource="Config\DBConnection.config"/>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
重點(自己看圖解釋):
Config\DBConnection.config中的根節點的名字要跟 app.config中 configSource的簽名的節點名字一模一樣,才能插入Config\DBConnection.config那個文件里的其他配置。以后修改我們只要修改Config\DBConnection.config中的就行了,如果你有多個需要配置,多建立幾個config文件,大架構的config一般都分離,方便維護
測試效果(Program.cs文件編寫代碼如下):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace TestConfig
{class Program
{static void Main(string[] args){//Demo1
string connectionString = ConfigurationManager.ConnectionStrings["TestDBConnectionString"].ToString();Console.WriteLine(connectionString);Console.ReadLine();}}}
效果如下
成功的!
2 . App.config中的自定義配置
2.1 基本樣子,自定義配置可分為:自定義配置節和自定義配置節組。
<configuration><configSections> //配置節聲明區域,包含配置節和命名空間聲明
<section> //配置節聲明
<sectionGroup/> //定義配置節組
</section> //配置節組中的配置節聲明
</configSections><appSettings/> //預定義配置節
<Custom element for configuration section> //配置節設置區域</configuration>
2.2 開始練習DEMO
(零)section有2塊,name和type,type如下
(一) SingleTagSectionHandler
①添加如下配置
注意 section的名字為InnerNetTest,接下來我們就可以添加一個InnerNetTest名稱的節點了,添加自定義的屬性,方便程序中使用
Program.cs開始文件測試
其他代碼:
IDictionary demo2 = ConfigurationManager.GetSection("InnerNetTest") as IDictionary; Console.WriteLine(string.Format(@"是否啟用{0} ip地址", demo2["enabled"], demo2["ip"]));運行發現會錯誤! 因為app.config中的configSections要放在頂部
我們調整配置的順序
<?xml version="1.0"?> <configuration> <configSections> <section name="InnerNetTest" type="System.Configuration.SingleTagSectionHandler"/> </configSections> <InnerNetTest enabled="true" ip="192.168.10.110:2000"/> <connectionStrings configSource="Config\DBConnection.config"/> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>效果:
成功了!
(二) SingleTagSectionHandler
我們添加第二個Section,添加key,value形式的值
<?xml version="1.0"?> <configuration> <configSections> <section name="InnerNetTest" type="System.Configuration.SingleTagSectionHandler"/> <section name="InnerNetCollection" type="System.Configuration.DictionarySectionHandler"/> </configSections> <InnerNetTest enabled="true" ip="192.168.10.110:2000"/> <InnerNetCollection> <add key="innerip" value="192.168.10.110:2000"/> <add key="pip" value="192.168.10.120"/> <add key="realip" value="192.168.10.150"/> </InnerNetCollection> <connectionStrings configSource="Config\DBConnection.config"/> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>Program.cs
//Demo3 IDictionary demo3 = ConfigurationManager.GetSection("InnerNetCollection") as IDictionary; foreach (DictionaryEntry e in demo3) { Console.WriteLine(string.Format("鍵{0} 值{1}", e.Key, e.Value)); }效果:
成功了!
(三) SingleTagSectionHandler
繼續在configSections添加<section>,最終如下
<?xml version="1.0"?> <configuration> <configSections> <section name="InnerNetTest" type="System.Configuration.SingleTagSectionHandler"/> <section name="InnerNetCollection" type="System.Configuration.DictionarySectionHandler"/> <section name="InnerNetCollection2" type="System.Configuration.NameValueSectionHandler"/> </configSections> <InnerNetTest enabled="true" ip="192.168.10.110:2000"/> <InnerNetCollection> <add key="innerip" value="192.168.10.110:2000"/> <add key="pip" value="192.168.10.120"/> <add key="realip" value="192.168.10.150"/> </InnerNetCollection> <InnerNetCollection2> <add key="innerip" value="hello"/> <add key="innerip" value="world"/> <add key="pip" value="AaronYang"/> <add key="realip" value="cnblogs"/> </InnerNetCollection2> <connectionStrings configSource="Config\DBConnection.config"/> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>Program.cs
//Demo4 //NameValueCollection demo4 = (NameValueCollection)ConfigurationManager.GetSection("InnerNetCollection2"); //這個方法已經過時,我們可以這樣寫 NameValueCollection demo4 =ConfigurationManager.GetSection("InnerNetCollection2") as NameValueCollection; Console.WriteLine(demo4.AllKeys[0].ToString()+" "+demo4["innerip"]);效果圖:
其實按我的理想應該是 Hello,world,但是結果不是,因為NameValueCollection集合是一個鍵對應多個string,如果鍵一樣,合並結果Hello,world,但是不是,求大神這樣的結果對嗎?
(四) sectionGroup
我們添加<sectionGroup>然后取一個名字,然后在里面寫<section>取個名字,然后在下面添加具體細節
<group的名字><group里面的section的名字><add key=... value=...>...<add key=... value=...></group里面的section的名字></group的名字>
<?xml version="1.0"?> <configuration> <configSections> <section name="InnerNetTest" type="System.Configuration.SingleTagSectionHandler"/> <section name="InnerNetCollection" type="System.Configuration.DictionarySectionHandler"/> <section name="InnerNetCollection2" type="System.Configuration.NameValueSectionHandler"/> <sectionGroup name="Students"> <section name="Student" type="System.Configuration.DictionarySectionHandler"/> </sectionGroup> </configSections> <InnerNetTest enabled="true" ip="192.168.10.110:2000"/> <InnerNetCollection> <add key="innerip" value="192.168.10.110:2000"/> <add key="pip" value="192.168.10.120"/> <add key="realip" value="192.168.10.150"/> </InnerNetCollection> <InnerNetCollection2> <add key="innerip" value="hello"/> <add key="innerip" value="world"/> <add key="pip" value="AaronYang"/> <add key="realip" value="cnblogs"/> </InnerNetCollection2> <Students> <Student> <add key="1000" value="王磊"/> <add key="1001" value="王類"/> <add key="1002" value="王雷"/> </Student> </Students> <connectionStrings configSource="Config\DBConnection.config"/> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>Program.cs調用
//Demo5 IDictionary demo5 = ConfigurationManager.GetSection(@"Students/Student") as IDictionary; foreach (DictionaryEntry e in demo5) { Console.WriteLine(string.Format("鍵:{0} 值:{1}", e.Key, e.Value)); }效果圖:
成功了!
(五) IConfigurationSectionHandler
新建一個類InnerConfigTest,實現 IConfigurationSectionHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Collections; using System.Xml; namespace TestConfig { public class InnerConfigTest : IConfigurationSectionHandler { private string enabled; public string Enabled { get { return enabled; } set { enabled = value; } } private string ip; public string IP { get { return ip; } set { ip = value; } } public object Create(object parent, object configContext, System.Xml.XmlNode section) { Hashtable hashtable = new Hashtable(); foreach (XmlAttribute attribute in section.Attributes) { hashtable[attribute.Name] = attribute.Value; } return hashtable; } } }我們接下來可以在app.config中添加配置
configSections中添加
<section name="InnerConfigTest" type="System.Configuration.SingleTagSectionHandler"/>然后對應的細節
<InnerConfigTest enabled="true" ip="192.168.10.110:8888"/>Program.cs
InnerConfigTest us = new InnerConfigTest(); Hashtable ht = ConfigurationManager.GetSection(@"InnerConfigTest") as Hashtable; us.Enabled = (string)ht["enabled"]; us.IP = (string)ht["ip"]; Console.WriteLine(us.Enabled+" "+us.IP);因為InnerConfigTest的Create方法返回的其實本質是Hashtable的object,所以用Hashtable接受,如果返回一個List也行,就看你怎么在Create方法里面怎么寫了,里面其實是一個XML的操作
這里我就暫時講到這里了
效果圖:
成功了!
一切我暫時先將這么多了,總結一下
①你可以知道怎樣讓app.config更加簡潔,通過分離部分的配置,通過configSource引入
②具體的某些自定義字典在XML配置的使用
③如何使用IConfigurationSectionHandler ,讓代碼顯得更高級
④你學到東西了!呵呵
⑤謝謝 C#實驗室 群里面的好朋友對我的支持,C#實驗室人已經滿50人了,如果你的技術確實不錯,或者你在C#方面有什么一技之長,都可私聊我,入群和我們共同玩耍,呵呵,我說不出肉麻的話,你們懂得哈!