现在的位置: 首页 > 综合 > 正文

在.NET框架下使用自定义配置设置(转)

2013年03月17日 ⁄ 综合 ⁄ 共 5023字 ⁄ 字号 评论关闭

.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.WriteLine((string)nvsh["key1"]);
    System.Diagnostics.Debug.WriteLine((string)dsh["key1"]);
    System.Diagnostics.Debug.WriteLine((string)sgnvsh["key1"]);

以下是用于上面代码的一段配置XML代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="MySingleTagSection"
 type="System.Configuration.SingleTagSectionHandler"/>
        <section name="MyDictionarySection"
 type="System.Configuration.DictionarySectionHandler"/>
        <section name="MyNameValueSection"
type="System.Configuration.NameValueSectionHandler"/>
        <sectionGroup name="MySectionGroup">
            <section name="MySection1"
 type="System.Configuration.NameValueSectionHandler"/>
            <section name="MySection2"
 type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
    </configSections>
    <MySingleTagSection sample1="value1" sample2="value2" sample3="value3"/>
    <MyDictionarySection>
        <add key="key1" value="value1"/>
        <add key="key2" value="value2"/>
    </MyDictionarySection>
    <MyNameValueSection>   
        <add key="key1" value="value1"/>
        <add key="key2" value="value2"/>
    </MyNameValueSection>
    <MySectionGroup>
        <MySection1>
            <add key="key1" value="value1"/>
            <add key="key2" value="value2"/>
        </MySection1>
        <MySection2>
            <add key="key1" value="value1"/>
            <add key="key2" value="value2"/>
        </MySection2>
    </MySectionGroup>
</configuration>

欲知更多信息,请访问MSDN Library中的application configuration settingsconfiguration schema

 

如何设置.net程序中配置文件的路径。
在开发.net程序时,经常会用到配置文件,应用程序的配置文件一般是MyApp.exe.config的形式,放在MyApp.exe的同级目录,但每次生成MyApp项目时,vs.net会自动删除MyApp.exe的同级目录下的MyApp.exe.config,这样调试程序时很不方便。能不能指定其他目录下的配置文件呢?
可以的,调用下面的方法即可。
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE","D://OtherDir//MyApp.exe.config");

抱歉!评论已关闭.