C#讀取Appconfig中自訂的節點

來源:互聯網
上載者:User

標籤:

  今天在使用Nlog的時候,發現了一個之前沒注意的問題。

 

  以前,我的app設定檔都是這麼寫的,當然配置比較多的時候會改用xml。

 

 如果<appSettings>節點中的內容很多的話,我自己有時候都分不清哪個是做什麼的,可能朋友們會說,你加個注釋不就行了。但是可不可以把一些相同的配置放在一起呢,就像上面的nlog一樣。先試著改造下設定檔

1     <configSections>2         <section name="mySection" type="ConfigSolution.ConfigSectionHandler,ConfigSolution"></section>3     </configSections>4     <mySection>5         <port CPort="40001" WPort="40002" SPort="50000"></port>6         <coustomAssembly CommandsAssembly="HX.Components.Command.Collection" CommandMessagesAssembly="HX.Components.CommandMessage.Collection"></coustomAssembly>7     </mySection>

  那麼,怎麼擷取section裡的值呢?從configSections 元素開始到網上風暴了一番。ConfigurationSection 類

 然後知道可以通過ConfigurationManager類的GetSection方法擷取到設定檔的資訊。(如果應用程式需要以唯讀方式訪問其自身配置,則對於 Web 應用程式,建議使用 GetSection() 重載方法;對於用戶端應用程式,建議使用 ConfigurationManager.GetSection 方法。----MSDN)

var mySection = ConfigurationManager.GetSection("mySection");

  運行一下程式試試,迎來了第一個異常。System.Configuration.ConfigurationErrorsException: 建立 mySection 的配置節處理常式時出錯: 類型“ConfigSolution.ConfigSectionHandler”不從“System.Configuration.IConfigurationSectionHandler”繼承。 ---> System.TypeLoadException: 類型“ConfigSolution.ConfigSectionHandler”不從“System.Configuration.IConfigurationSectionHandler”繼承。

  既然說我的ConfigSolution.ConfigSectionHandler不從System.Configuration.IConfigurationSectionHandler繼承,那好,我就繼承它,然後看看這個介面都有些什麼東西,Ctrl+T一下(SharpDevelop的快速鍵),這介面就一個方法 

直接MSDN一下,IConfigurationSectionHandler.Create  資訊量不是很大,就一句話:IConfigurationSectionHandler.Create 方法,建立配置節處理常式。算了,直接斷點跟蹤一下,果然有東西

 

好了,剩下的就是對xml的讀取了。直接把section return看看,

 

這回程式正常運行了,且mySection 也拿到了設定檔

 

但是在程式中我們怎麼擷取這些配置資料呢?我建立了一個處理設定檔的MySectionHelper類,大體如下

 1     public class MySectionHelper 2     { 3         readonly XmlNode _section; 4         readonly XmlNode _coustomAssembly; 5         public MySectionHelper(XmlNode section) 6         { 7             _section=section; 8             _coustomAssembly= _section.SelectSingleNode("coustomAssembly"); 9         }10         11         public string CommandsAssembly{get{return _coustomAssembly.Attributes["CommandsAssembly"].Value;}}12     }

試試行不行,我的設定檔

1     <configSections>2         <section name="mySection" type="ConfigSolution.ConfigSectionHandler,ConfigSolution"></section>3     </configSections>4     <mySection>5         <port CPort="40001" WPort="40002" SPort="50000"></port>6         <coustomAssembly CommandsAssembly="HX.Components.Command.Collection" CommandMessagesAssembly="HX.Components.CommandMessage.Collection"></coustomAssembly>7     </mySection>

運行結果:

好了,一切完成。然後到網上去找找看看有沒有更多的資料,果然很多

子陽哥寫了一篇《.Net 自訂應用程式配置》大家可以去看看。

MSDN:https://msdn.microsoft.com/zh-cn/sqlserver/ms228056(v=vs.71).aspx

C#讀取Appconfig中自訂的節點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.