网站建设
  简约型网页设计套餐998
  实惠型网站建设套餐2580
  综合型网站制作套餐4980
  网站改版与网站维护
  行业网站建设方案
  大型网站建设解决方案
  企业网站建设流程
  帝网科技网站设计与网站制作
建站FAQ
·网站空间问题解答
·企业邮箱问题解答
 
酷站欣赏
·房产酷站(379)
·综合门户(8 9)
·建筑装饰(603)
·手机通讯(354)
·生活购物(376)
·医疗保健(199)
·文化摄影(602)
·休闲体育(399)
>>更多酷站欣赏
网站优化
·Google(谷歌)优化   ·百度(BaiDu)优化
·雅虎(Yahoo)优化    ·Alexa排名优化   
·Google AdSense   ·DMOZ目录提交  
建站知识
·网站建设知识·网站名词解释·网站运营知识
·网络营销知识·搜索引擎知识·实用技术文摘
网站推广
百度网站推广 google网站推广
搜狐网站推广 网易网站推广
新浪网站推广   雅虎网站推广
  您当前位置: 当前位置:帝网科技 >> web开发 >> .NET专栏 >> 浏览文章
 
 
在.NET框架下使用自定义配置设置
作者:袁袁整理 来源:帝网科技 日期:2008年11月01日 点击数:



.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"]);

  相关文章
 
·ASP.NET使用log4Net日志组件教程(日志
·ASP.NET MVC 框架
·C#实现的BinaryTree
·WebForms使用System.Web.Routing
·ASP.NET获取远程网页下载到本地文件
·一个“简单”的ASP.NET的服务器控件
·ASP.net与PHP两大网站开发架构优势对比
·教你七招提高.NET网站性能
·ASP.NET未来:简化开发 HTML5性能提升
·ASP.NET实现类似Excel的数据透视表
·FileUpload上传多文件出现错误的解决方
·.NET从优酷专辑中采集所有视频及信息(
·ASP.NET 4中的SEO改进
·详解Asp.net MVC DropDownLists
·提高ASP.NET应用程序性能的几招方法
·asp.net实现51job地区选择效果
·ASP.NET中创建GeoRSS订阅源
·ASP.NET 4.0开发更加简便
·ASP.NET页面间数据传递的方法
·ASP.NET的SEO:使用.ashx文件——排除
 
 

公司环境 | 合作伙伴 | 人才招聘 | 付款方式 | 关于我们

地址:广州市天河区中山大道中120号D805 电话:020-82529556 传真:020-82529556
广州帝网网络科技有限公司 版权所有 粤ICP备08119341号