配置文件中使用Custom Sections
在config文件中添加如下configSections
<?xml version="1.0" encoding="Windows-1252"?> <configuration> <configSections> <section name="FavoriteUrls" type="System.Configuration.NameValueSectionHandler" /> </configSections> <appSettings> </appSettings> <FavoriteUrls> <add key="Microsoft" value="http://www.microsoft.com/" /> <add key="DotNetSpider" value="http://www.DotNetSpider.com/" /> <add key="AsianSpider" value="http://www.AsianSpider.com/" /> </FavoriteUrls> </configuration>
在C#代码中访问配置文件:
1。 添加命名空间:
using System.Collections.Specialized; using System.Configuration;
2。访问代码:
NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("FavoriteUrls");
for (int i = 0; i < section.Keys.Count; i++) { string key = section.Keys[i]; string name = section.GetValues (key)[0]; // .GetValues() returns a collection of values. In our case, we have //only one value, so get the first one. }
来自:http://www.dotnetspider.com/resources/14-Custom-sections-config-file.aspx
Note: Cross posted from Bookcold's Blog.Permalink
浙公网安备 33010602011771号