應用程式設定檔

來源:互聯網
上載者:User
應用程式要在一台電腦上運行,可能需要進行一些配置。CLR的設定檔是XML檔案。應用程式可以在不進行重新編譯的情況下使用設定檔。這些設定檔是用XML編寫的,元素本身的自描述性優於傳統的基於文本的設定檔。.NET支援多種設定檔:機器層級、應用程式層級、直接與安全性相關等。其中最常用的就是應用程式層級的設定檔。這篇文章簡要敘述應用程式設定檔的使用方法。 應用程式設定檔的檔案名稱是可知性檔案的檔案名稱加.config。如果應用程式名稱為App.exe,那麼其設定檔應該為App.exe.config檔案。應該放在/bin檔案夾下。 在應用程式設定檔裡面最有用處的,也是最常用的部分其實是自訂配置部分。設定檔架構允許在設定檔中存在使用者自訂的段落,可以在這些自訂的段落中安排配置資料。.NET架構類庫提供一個供應用程式使用的通用配置處理器,包括兩個類:System.Consiguration.SingleTagSectionHandler和System.Configuration.NameValueSectionHandler。 用一個執行個體來說明問題是最好的辦法。首先,建立一個控制台項目名為ReadCustomData。然後,在解決方案管理器裡,右擊項目,添加,添加新項,選擇應用程式設定檔。命名為:ReadCustomData.exe.config。檔案內容如下: <?xmlversion="1.0"encoding="utf-8"?><configuration>     <configSections>         <sectionname="customSection"              type="System.Configuration.SingleTagSectionHandler"/>     </configSections>     <customSectionsetting1="one"                       setting2="two"                       setting3="three"/></configuration> 然後再Main函數裡寫下如下程式: static void Main(string[] args)         {              //              // TODO: 在此處添加代碼以啟動應用程式              //              //從設定檔或的資料              IDictionary sampleTable = (IDictionary)                   ConfigurationSettings.GetConfig("customSection");              Console.WriteLine(sampleTable["setting1"].ToString());              Console.WriteLine(sampleTable["setting2"].ToString());              Console.WriteLine(sampleTable["setting3"].ToString());               Console.ReadLine();         }執行結果:onetwothree 注意:一定要把設定檔放置在/bin/debug目錄下。 還有另外一個選項解析類System.Configuration.NameValueSectionHandler。要在設定檔裡使用這個,設定檔和程式就應當這樣寫: <?xmlversion="1.0"encoding="utf-8"?><configuration>     <configSections>         <sectionname="customNamedSection"              type="System.Configuration.NameValueSectionHandler"/>     </configSections>     <customNamedSection>         <addkey="setting1"value="one"/>         <addkey="setting2"value="two"/>         <addkey="setting3"value="three"/>     </customNamedSection></configuration> 程式:static void Main(string[] args)         {              //              // TODO: 在此處添加代碼以啟動應用程式              //              //從設定檔得到資料              NameValueCollection sampleTable = (NameValueCollection)                   ConfigurationSettings.GetConfig("customNamedSection");              Console.WriteLine(sampleTable["setting1"]);              Console.WriteLine(sampleTable["setting2"]);              Console.WriteLine(sampleTable["setting3"]);                            Console.ReadLine();         }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.