使用ConfigurationManager来写自己的配置文件

 

在.NET 1.1下,你必须用过实现IConfigurationSectionHandler接口来进行操作。

但在.NET 2.0下,微软提供了一系列可以对配置文件进行操作的方法,而且非常强大,能够让你随便定义自己的配置节点。在开始之前我们先看看我们想写一个怎样的配置节:

None.gif    < configSections >
None.gif    
< section  name ="MailSettings"  type ="MyBlog.MailSection, MyBlog"   />
None.gif  
</ configSections >

这个地方指定了我们想要写一个MailSettings的节,后面的type标识将会使用MyBlog这个assembly理的MyBlog.MailSection类来识别这个section。


None.gif < MailSettings >
None.gif    
< MailPlugins >
None.gif      
< add  name ="Server"  value =""   />
None.gif      
< add  name ="Title"  value =""   />
None.gif      
< add  name ="Body"  value =""   />
None.gif    
</ MailPlugins >
None.gif  
</ MailSettings >

这个地方有是我们自定义的一些配置了,value就是我们所需要的一些值。

现在目标已经有了,下面就是怎么去用这些API来读出我们需要的值:

首先很自然我们需要MailSettings的这个section,于是我们定义类:

None.gif      public   sealed   class  MailSection : ConfigurationSection
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
ExpandedBlockEnd.gif    }

一个ConfigurationSection可以有自己的一些attribute,这些网上资料很多,我就不详解了。我关心的是下面的MailPlugins这层,这层在配置文件上被称为ConfigurationElementCollection,也就是一些ConfigurationElement的集合。那么我们先定义所需要的ConfigurationElement:

None.gif      public   sealed   class  MailPluginElement : ConfigurationElement
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        [ConfigurationProperty(
"name", IsRequired = true, IsKey = true)]
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (string)this["name"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis["name"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [ConfigurationProperty(
"value", IsRequired = true)]
InBlock.gif        
public string Value
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (string)this["value"]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis["value"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }


 接着是定义这个ConfigurationElement的集合:

None.gif      public   sealed   class  MailPluginElementCollection : ConfigurationElementCollection
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
protected override ConfigurationElement CreateNewElement()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new MailPluginElement();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override object GetElementKey(ConfigurationElement element)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ((MailPluginElement)element).Name;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

默认的抽象类是让我们必须override上面的两个保护方法。但是这样是不能满足我们的要求的,我们希望用add key 和 value这种形式来配置:

None.gif          public   override  ConfigurationElementCollectionType CollectionType
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ConfigurationElementCollectionType.AddRemoveClearMap;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

 这里相当重要!需要使用AddRemoveClearMap来确保配置处理节能够识别<add key="" value="" />,如果配置为别的会报给你add节无法识别的错误!

那么最后我们一个常用的功能没有提供:索引器!于是我们再加上:

None.gif          public  MailPluginElement  this [ int  index]
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn BaseGet(index) as MailPluginElement; }
ExpandedBlockEnd.gif        }

None.gif
None.gif        
public   new  MailPluginElement  this [ string  name]
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn BaseGet(name) as MailPluginElement; }
ExpandedBlockEnd.gif        }

那么ConfigurationElementCollection告一段落了,现在就是把这些东西塞进ConfigurationSection了:

None.gif      public   sealed   class  MailSection : ConfigurationSection
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        [ConfigurationProperty(
"MailPlugins")]
InBlock.gif        
public MailPluginElementCollection MailPlugins
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (MailPluginElementCollection)base["MailPlugins"]; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

注意这里的ConfigurationProperty这个Attribute,里面的字符串就是你在配置节里的那个element group节点。当然如果你想省事的话你可以定义为"",这样上面的配置文件就变为:

None.gif < MailSettings >
None.gif      
< add  name ="Server"  value =""   />
None.gif      
< add  name ="Title"  value =""   />
None.gif      
< add  name ="Body"  value =""   />
None.gif  
</ MailSettings >

最后便是如何使用了:

None.gif          public   string  GetConfigValue( string  name)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            MailSection mailSection 
= System.Configuration.ConfigurationManager.GetSection("MailSettings"as MailSection;
InBlock.gif
InBlock.gif            MailPluginElementCollection mailInfos 
= mailSection.MailPlugins;
InBlock.gif
InBlock.gif            
return mailInfos[name].Value;
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/gamix/archive/2007/01/29/633403.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值