热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » ASP/ASP.NET编程 » 在.NET框架下使用自定义配置设置

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

论坛链接
  • 在.NET框架下使用自定义配置设置
  • 发布时间:2008-11-01 10:21:06    浏览数:6592    发布者:tznktg    设置字体【   
.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>
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理