編寫者:鄭昀@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");來讀取了。