[Config]如何利用ConfigurationSettings.AppSettings.GetValues讀取設定檔中多個同Key的value

來源:互聯網
上載者:User

編寫者:鄭昀@Ultrapower

預設情況下,string[] strArray = System.Configuration.ConfigurationSettings.AppSettings.GetValues("Uri");是無法讀取設定檔中多個同Key的value的。如下所示的配置:

<appSettings>

  

  <add key="Uri" value="uri1"/>

  <add key="Uri" value="uri2"/>

  <add key="Uri" value="uri3"/>


 </appSettings> 用MSDN告訴我們的GetValues是讀不到的,只能讀到最後一個value。

 http://www.codeproject.com/dotnet/namevaluemultiple.asp告訴我們,只有這麼做才可以:第一步:單獨建立一個類庫MultipleSectionHandler,把NameValueMultipleSectionHandler.cs加進去,並將MultipleSectionHandler.csproj加入到我們的工程中; 第二步:編譯MultipleSectionHandler,產生MultipleSectionHandler.dll; 第三步:將WebApp應用的Web.config檔案中加入<configSections>

  <remove name="appSettings" />

  <section name="appSettings" type="MyCompany.Configuration.NameValueMultipleSectionHandler, MultipleSectionHandler" />

 </configSections>表明對於appSettings的讀取將採用我們自己的MultipleSectionHandler處理。 第四步:這時候就可以針對Web.config中的:<appSettings>

  

  <add key="Uri" value="uri1"/>

  <add key="Uri" value="uri2"/>

  <add key="Uri" value="uri3"/>

 </appSettings>通過string[] strArray = System.Configuration.ConfigurationSettings.AppSettings.GetValues("Uri");來讀取了。

聯繫我們

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