.Net設定檔常用配置說明

來源:互聯網
上載者:User

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

設定檔內容如下:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

     <configSections>      

         <section name="mySection" type="System.Configuration.NameValueSectionHandler"/>

         <section name="mySingleTagSection" type="System.Configuration.SingleTagSectionHandler"/>

         <section name="myDictionarySection" type="System.Configuration.DictionarySectionHandler"/>

 

         <sectionGroup name="mySections">

              <section name="mySection1" type="System.Configuration.NameValueSectionHandler"/>

              <section name="mySection2" type="System.Configuration.NameValueSectionHandler"/>

         </sectionGroup>

     </configSections>

    

    <mySection>

         <add key="key1" value="value1" />

        <add key="key2" value="value2" />

    </mySection>

   

    <mySingleTagSection setting1="value1" setting2="value2" setting3="value3" />

 

     <myDictionarySection>

         <add key="DictionarySectionKey1" value="DictionarySectionValue1"/>

         <add key="DictionarySectionKey2" value="DictionarySectionValue2"/>

         <add key="DictionarySectionKey3" value="DictionarySectionValue3"/>

     </myDictionarySection>

       

     <mySections>

         <mySection1>

              <add key="mySection1_key1" value="mySection1_value1" />

              <add key="mySection1_key2" value="mySection1_value2" />         

         </mySection1>

         <mySection2>

              <add key="mySection2_key1" value="mySection2_value1" />

              <add key="mySection2_key2" value="mySection2_value2" />

         </mySection2>

     </mySections>

    

     <appSettings>

        <add key="appkey1" value="appvalue1" />

        <add key="appkey2" value="appvalue2" />   

    </appSettings>

</configuration>

代碼片斷如下:

private static void TestAppSettings()

{

     Console.WriteLine("TestAppSettings ...");

 

     NameValueCollection config = ConfigurationSettings.AppSettings;

    

     Console.WriteLine("value1:" + config["key1"]);

     Console.WriteLine("value2:" + config["key2"]);

 

     Console.WriteLine();

}

private static void TestSection()

{

     Console.WriteLine("TestSection ...");

 

     NameValueCollection config ;

     config = (NameValueCollection)ConfigurationSettings.GetConfig("mySection");

     Console.WriteLine("value1:" + config["key1"]);

     Console.WriteLine("value2:" + config["key2"]);

 

     Console.WriteLine();

}

 

private static void TestSingleTagSection()

{

     Console.WriteLine("TestSingleTagSection ...");

 

     IDictionary dic ;

     dic = (IDictionary)ConfigurationSettings.GetConfig("mySingleTagSection");

     Console.WriteLine("value1:" + dic["setting1"]);

     Console.WriteLine("value2:" + dic["setting2"]);

     Console.WriteLine("value3:" + dic["setting3"]);

 

     Console.WriteLine();

}

 

private static void TestDictionarySection()

{

     Console.WriteLine("TestDictionarySection ...");

 

     IDictionary dic ;

     dic = (IDictionary)ConfigurationSettings.GetConfig("myDictionarySection");

     Console.WriteLine("DictionarySectionValue1:" + dic["DictionarySectionKey1"]);

聯繫我們

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