日期:2010-05-17  浏览次数:20408 次

.NET框架通过基于XML的配置使配置设置驾轻就熟。它同时还提供了必要的方法,通过集合类(Collection classes)访问这些设置。

通过一个静态的ConfigurationSettings类可访问实际的配置数据。该类还提供了一个GetConfig()方法,可向一个合适的集合返回一个对象。本文中,我将示范三种可用来访问和存储配置信息的方法。

应用配置数据存储在App.config文件,并由configSections节点定义。每一section都有一个type属性定义。这里我将讨论的3个类型为NameValueSectionHandler、SingleTagSectionHandler和DictionarySectionHandler。你可以用一个sectionGroup元素定义节组。以下是一个配置节定义的例子:

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

建议使用type="System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"

节组是嵌入一个sectionGroup元素的独立配置节。以下是一个节组的例子:

<sectionGroup name="CustomGroup">
<section name="Custom1"
type="System.Configuration.NameValueSectionHandler"/>
<section name="Custom2" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>

最后,你所指定的配置节将用于构造存储配置数据的自定义的XML节点。若要向配置节添加数据,只要将该配置节作为一个XML节点包含进去,并用add节点添加Collection数据。下例为一个NameValueSectionHandler配置节:

<MyCustomSection>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
</MyCustomSection>

MyCustomSection程序段包含一个命名值集合,其两个入口由key1和key2定义。

SingleTagSectionHandler较容易构造。正如NameValueSectionHandler,配置节可在configSections节点中找到。但在SingleTagSectionHandlers和NameValueSectionHandlers中,配置数据的添加方式是不同的,如下所示:

. . .
<section name="MySingleTagSection"
type="System.Configuration.SingleTagSectionHandler"/>
. . .
<MySingleTagSection setting1="value1" setting2="value2" setting3="value3"/>
. . .

DictionarySectionHandler与NameValueSectionHandler相似,但前者返回hashtable,后者返回NameValueCollection。当访问大量配置值时,hashtable要快于NameValueCollectio。DictionarySectionHandler与NameValueSectionHandler的构造方式相同,如下例:

. . .
<section name="MyDictionarySection"
type="System.Configuration.DictionarySectionHandler"/>
. . .
<MyDictionarySection>
<add key="key1" value="value1"/>
</MyDictionarySection>
. . .

我自己用了一下,报错。。原因还挺特别

System.Configuration.DictionarySectionHandler,System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089

没有办法把type改为以上那段。终于行了。

构造节组的方法与构造单独配置节的方法基本相同,唯一的不同在于前者的自定义节点互相嵌套。借用前面的节组定义,以下是对节组的实现:

<CustomGroup>
<Custom1>
<add key="key1" value="value1"/>
</Custom1>
<Custom2>
<add key="key1" value="value1"/>
</Custom2>
</CustomGroup>

通过System.Configuration.ConfigurationSettings命名空间的GetConfig()方法和自定义配置节的串值来访问应用配置设置,然后将该方法的结果转为合适的类型。

对于SingleTagSectionHandler,将结果转为System.Collections命名空间的IDictionary接口类型。对于NameValueSectionHandler,结果转为在System.Collections.Specialized命名空间中定义的NameValueCollection类型。最后,对于DictionarySectionHandler,结果转为System.Collections命名空间中的Hashtable类型。

对于节组,唯一的区别是,将加上正斜杠和配置节名的节组名作为字符串参数传递给GetConfig()方法,以访问自定义设置。

以下是一个使用这些自定义设置的实例:

    System.Collections.IDictionary stsh = (System.Collections.IDictionary)
System.Configuration.ConfigurationSettings.GetConfig("MySingleTagSection");
    System.Collections.Specialized.NameValueCollection nvsh =
 (System.Collections.Specialized.NameValueCollection)
 System.Configuration.ConfigurationSettings.GetConfig("MyNameValueSection");
    System.Collections.Hashtable dsh = (System.Collections.Hashtable)
 System.Configuration.ConfigurationSettings.GetConfig("MyDictionarySection");
    System.Collections.Specialized.NameValueCollection sgnvsh =
 (System.Collections.Specialized.NameValueCollection)
 System.Configuration.ConfigurationSettings.GetConfig("MySectionGroup/MySection
1");
    System.Diagnostics.Debug.WriteLine((string)stsh["sample1"]);
    System.Diagnostics.Debug.WriteLin