如何使用.NET配置文件(二)

本文探讨了如何使用.NET配置文件扩展配置结构,包括如何读取自定义配置信息,通过创建自定义配置读取类(ConfigurationSectionHandler)来访问配置结构,以及如何在配置文件中声明和定义这些自定义配置。
.NET的应用程序配置文件,使用的是XML格式。相对INI文件来说,它的功能要强上不少,而且具有很强的可扩展性。它的缺点是不能直接进行写操作,也就是说,不能直接在程序中修改配置文件的数据(当然不是指不能,不过不是本文讨论的范围)。本文主要目的是探讨如何扩展配置文件,并在其加入各种自定义配置信息。

如何使用.NET配置文件(一)    如何使用.NET配置文件(二)    沐枫网志


    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)
ExpandedBlockStart.gifContractedBlock.gif
{
 Console.WriteLine(
"Area={0} Device={1} Customer={2}", cfg["Area"], cfg["Device"], cfg["Customer"]);
}


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

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

   Hashtable item 
= new Hashtable();
   
foreach(XmlAttribute attr in node.Attributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
{
    
switch(attr.Name)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
     
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来实现任意的配置文件结构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值