当大家先后中采纳布置文件时,Asp.net中使用是Web.config,WinForm和Console中应用是App.config。平日用的最多是AppSettings节,有的时候以为不够用,另1抉择就是友好完结SectionHandler,来促成自定义配置节。看上面包车型客车示例AppSettings节:

<appSettings>
  <add key="source1user" value="user" />
  <add key="source1password" value="pass" />
</appSettings>

对此地点八个布局项,要写个自定义配置类是还是不是很辛勤,大家还有另壹种不难完结情势,使用
SingleTagSectionHandler

MSDN是那般描述的:Handles configuration sections that are represented
by a single XML tag in the .config file”.

上面来看一下:

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

<remoteDataSource username="user" password="pass" url="http://remote/" />

代码中那样访问:

      [Test]
      public void TestConfig()
      {
          Hashtable remoteDataSource =
(Hashtable)ConfigurationManager.GetSection("remoteDataSource");
          string username = (string)remoteDataSource["username"];
          string password = (string)remoteDataSource["password"];
          string url = (string)remoteDataSource["url"];

          Assert.AreEqual("user", username);
          Assert.AreEqual("pass", password);
      }

很简单吧。还有另一种简单配置节的方法,可参见 简单实现自定义配置节 这篇Post.

Author: Petter Liu     http://wintersun.cnblogs.com 

相关文章

网站地图xml地图