首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> .NET Framework >> 正文

讲述如何使用.NET的配置文件

    把上面的声明段放入配置文件中,我们的配置结构就可以正常使用了。声明中,用来定义不含配置数据的节的名称。

用来定义含有自定义配置数据的节的名称。
用来指定定义配置数据的类型。.NET已经定义了3种配置类型:
  a. NameValueSectionHandler
        相应访问代码如下: 

NameValueCollection myNameValue= (NameValueCollection)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myNameValue");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

  b. DictionarySectionHandler
        相应访问代码如下:

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myDictionary");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

  c. SingleTagSectionHandler
        相应访问代码如下:   

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myInfo");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

        这三种类型的详细信息,可以参考 MSDN 文档。同时.NET 还定义了IgnoreSectionHandler类型,为 System.Configuration 之外的系统所读取和处理的配置节提供节处理程序定义。
        除此之外,.NET提供了IConfigurationSectionHandler接口,这样我们还可以自行进行扩展,以设计出我们自已的配置形式。

        3. 自定义配置结构 (使用IConfigurationSectionHandler)
 假设有以下的配置信息,其在MyInfo可以重复许多次,那么应如何读取配置呢?这时就要使用自定义的配置程序了。
<myConfigs>
  
<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
  
<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
myConfig>

 访问代码如下:

Hashtable cfgTable = (Hashtable)ConfigurationSettings.GetConfig( "myConfigs" );

Debug.Assert( cfgTable.Count 
== 2);
Hashtable cfgFuzhou 
= (Hashtable)cfgTable["Fuzhou"];
Hashtable cfgShanghai 
= (Hashtable)cfgTable["Shanghai"];
Debug.Assert( cfgFuzhou[
"Device"== "Printer" );
Debug.Assert( cfgShanghai[
"Device"== "Mobile" );
Debug.Assert( cfgFuzhou[
"Customer"== "Muf" );
Debug.Assert( cfgShanghai[
"Customer"== "Liny" );

foreach(Hashtable cfg in cfgTable.Values)
{
 Console.WriteLine(
"Area={0} Device={1} Customer={2}", cfg["Area"], cfg["Device"], cfg["Customer"]);
}


 为了能使用上面的访问代码来访问配置结构,我们需要生成一个特定的配置读取类(ConfigurationSectionHandler),例子很简单,就不多做说明了:

public class MyInfoSectionHandler: IConfigurationSectionHandler
{
 
public object Create(object parent, object configContext, System.Xml.XmlNode section)
 
{
  Hashtable config 
= new Hashtable();
  
foreach(XmlNode node in section.ChildNodes)
  
{
   
if(node.Name != "myInfo")
    
throw new System.Configuration.ConfigurationException("不可识别的配置项", node);

   Hashtable item 
= new Hashtable();
   
foreach(XmlAttribute attr in node.Attributes)
   
{
    
switch(attr.Name)
    
{
     
case "Area":
     
case "Device":
     
case "Customer":
      item.Add(attr.Name, attr.Value);
      
break;
     
default:
      
throw new System.Configuration.ConfigurationException("不可识别的配置属性", attr);
    }

   }

   config.Add(item[
"Area"], item);
  }

  
return config;
 }

}


 然后,我们再定义配置说明。其中,myNamespace.MyInfoSectionHandler 是MyInfoSectionHandler类的带名字空间的完整名称;myApp 则是定义MyInfoSectionHandler类的程序集不带扩展名的名字(如myApp.dll或myApp.exe):

xml version="1.0" encoding="utf-8"?>
<configuration>
  

  
<configSections>
      
<section name="myConfig" type="myNamespace.MyInfoSectionHandler, myApp" />
  
configSections>   
  
<myConfigs>
    
<myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
    
<myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
  
myConfig>
configuration>

 根据上面的例子,我们可以使用IConfigurationSectionHandler来实现任意的配置文件结构。

上一页  [1] [2] 

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航