配置文件读取类

今天研究了一下Duwamish的配置文件的读取方法

首先 web.Config文件中:
None.gif <? xml version="1.0" encoding="utf-8" ?>
None.gif
< configuration > None.gif
None.gif  
< configSections >
None.gif    
< section  name ="TempConfiguration"  type ="Temp.Config.TempConfiguration, Temp"   />
None.gif  
</ configSections >   
None.gif  
< TempConfiguration >
None.gif    
< add  key ="Temp"  value ="aaaaaaa"   />
None.gif  
</ TempConfiguration >
None.gif
</ configuration >

其中 TempConfiguration是配置文件的一段,他对应的配置文件类是 type ="Temp.Config.TempConfiguration, Temp"  。其中 Temp.Config.TempConfiguration是具体的类文件, , Temp是该类所在的DLL的名字。还有其他可选参数,例如版本号,公钥等。

配置文件写好以后,还要写读取这个配置的对应的类,也就是 Temp.Config.TempConfiguration类。
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Configuration;
None.gif
using  System.Xml;
None.gif
using  System.Collections.Specialized;
None.gif
None.gif
namespace  Temp.Config
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// TempConfiguration 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class TempConfiguration : IConfigurationSectionHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private const string TEMP = "Temp";
InBlock.gif        
private static string temp;
InBlock.gif        
private const string TEMP_DEFAULT = "aaa";
InBlock.gif        
public static string Temp
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return temp;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Object Create(Object parent, object configContext, XmlNode section)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{                    
InBlock.gif            NameValueCollection settings;                    
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NameValueSectionHandler baseHandler 
= new NameValueSectionHandler();
InBlock.gif                settings 
= (NameValueCollection)baseHandler.Create(parent, configContext, section);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                settings 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif        
InBlock.gif            
if (settings == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                temp 
= TEMP_DEFAULT;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                temp 
= ReadSetting(settings, TEMP, TEMP_DEFAULT);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return settings;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
函数 ReadSetting#region 函数 ReadSetting
InBlock.gif        
InBlock.gif        
public static String ReadSetting(NameValueCollection settings, String key, String defaultValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Object setting 
= settings[key];
InBlock.gif                
InBlock.gif                
return (setting == null? defaultValue : (String)setting;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return defaultValue;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool ReadSetting(NameValueCollection settings, String key, bool defaultValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Object setting 
= settings[key];
InBlock.gif                
InBlock.gif                
return (setting == null? defaultValue : Convert.ToBoolean((String)setting);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return defaultValue;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static int ReadSetting(NameValueCollection settings, String key, int defaultValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Object setting 
= settings[key];
InBlock.gif                
InBlock.gif                
return (setting == null? defaultValue : Convert.ToInt32((String)setting);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return defaultValue;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion
 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

“.net规定,所有能够处理配置节的类必须要实现IConfigurationSectionHandler接口,而IConfigurationSectionHandler接口很简单,只有一个object Create(object parent,object configContext,XmlNode section)方法,这个方法不需要主动调用,它是在ConfigurationSettings.GetConfig这个静态方法的时候自动调用的,也就是说,当你在程序中使用ConfigurationSettings.GetConfig来获取配置节的时候,.net会根据改配置节声明中所定义的类名和路径自动实例化配置节处理类,并调用Create方法。” Duwamish深入剖析-配置篇

然后在(web)程序启动的时候初始化该类:
None.gif void  Application_OnStart()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {       System.Configuration.ConfigurationSettings.GetConfig("TempConfiguration");
ExpandedBlockEnd.gif}

这样,在程序的其他地方,只要使用Temp.Config.TempConfiguration.Temp 就可以得到配置文件(Web.Config)中Temp的值,不需要再去读取配置文件,可以提高速度。而且,如果有比较特殊的类型,例如bool甚至枚举(int)都可以直接读取,无需做类型转换。也不会读到null值(配置文件读取的类中有默认值)。

转载于:https://www.cnblogs.com/zellzhang/archive/2005/07/12/191381.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值