RFID会议签到系统总结(六)――系统配置(下)

本文介绍了一种自定义配置文件读写的方法,通过创建基类ConfigIOBase实现了配置文件的读取和写入功能。文章详细解释了如何通过XML操作获取配置信息,并提供了GetConfig方法用于读取配置,同时介绍了Write方法用于更新配置。

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

关于读配置,上次还漏说了一种情况,就是配置文件不是通常的由app.config生成的,其名字与程序集名字不相关的的时候,我们得自己写类似于GetConfig的方法来读取。这个种时候与写配置文件的功能有相通之处,所以放在这一篇里讲。

先来个读写配置文件的基类ConfigIOBase

ContractedBlock.gifExpandedBlockStart.gif读写配置文件的基类
None.gif    public class ConfigIOBase
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{      
InBlock.gif
InBlock.gif       
private XmlNode handlerSections;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif       
protected XmlDocument doc;
InBlock.gif
InBlock.gif       
protected XmlNode cfgSection;
InBlock.gif
InBlock.gif       
protected string configPath;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif       
public ConfigIOBase(string path)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif
InBlock.gif           configPath 
= path;
InBlock.gif
InBlock.gif           doc 
= new XmlDocument();
InBlock.gif
InBlock.gif           doc.Load(path);
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif           cfgSection 
= doc.SelectSingleNode("/configuration");
InBlock.gif
InBlock.gif           handlerSections 
= cfgSection.SelectSingleNode("//configSections");
InBlock.gif
ExpandedSubBlockEnd.gif       }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif
InBlock.gif       
/// 根据配置节得其处理程序
InBlock.gif
InBlock.gif       
/// </summary>
InBlock.gif
InBlock.gif       
/// <param name="sectionName">节名</param>
InBlock.gif
ExpandedSubBlockEnd.gif       
/// <returns></returns>

InBlock.gif
InBlock.gif       
protected CustomSectionHandlerBase getHandler(string sectionName)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif
InBlock.gif           
try
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif
InBlock.gif              XmlNode section 
= handlerSections.SelectSingleNode("//section[@name='" + sectionName + "']");
InBlock.gif
InBlock.gif              
string typeName = section.Attributes["type"].Value;
InBlock.gif
InBlock.gif              Type handlerType 
= Type.GetType(typeName);
InBlock.gif
InBlock.gif              BindingFlags bf 
= BindingFlags.Instance | BindingFlags.Public;
InBlock.gif
InBlock.gif              
object handler = handlerType.InvokeMember(null,bf | BindingFlags.CreateInstance,null,null,null);
InBlock.gif
InBlock.gif              
return handler as CustomSectionHandlerBase;
InBlock.gif
ExpandedSubBlockEnd.gif           }

InBlock.gif
InBlock.gif           
catch(Exception)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif
InBlock.gif              
return null;
InBlock.gif
ExpandedSubBlockEnd.gif           }

InBlock.gif
ExpandedSubBlockEnd.gif       }

InBlock.gif
InBlock.gif       
InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif

 

这里其实没什么奥秘,就是一些XML方面的读取,从name读到type名,然后利用反射创建Handler,然后就可以进行下面的处理。

比如读取配置,就可以如下面这样了

       public object GetConfig(string sectionName)

       {

           CustomSectionHandlerBase handler = getHandler(sectionName);

           if (handler != null)

           {

              return handler.Create(null,null,cfgSection.SelectSingleNode("//" + sectionName));

           }

           return null;

       }

.Net Framework里的GetConfig方法其实也是如上述这样做的。而写配置则纯粹就是用有关XML的操作的。

       /// <summary>

       /// 写配置节

       /// </summary>

       /// <param name="sectionName">节名</param>

       /// <param name="sectionValue">要写的内容</param>

       public void Write(string sectionName,object sectionValue){

           CustomSectionHandlerBase handler = getHandler(sectionName);

           if (handler != null){

              try{

                  XmlNode writeData = handler.SetSection(sectionValue);

                  XmlNode repData = doc.ImportNode(writeData,true);

                  cfgSection.ReplaceChild(repData,cfgSection.SelectSingleNode("//" + sectionName));

              }catch(Exception){

              }

           }

       }


Handler
SetSection方法在上一篇里有说明。

最后不要忘了保存文件,

public void Save(){

           if ((File.GetAttributes(configPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){

              File.SetAttributes(configPath,FileAttributes.Normal);

           }

           doc.Save(configPath);

       }

 

系统开发中在设计阶段不可能面面倶到,在实现阶段有时不得不新增一些配置,但又不想去为此兴师动众的新增配置类、新增handler,而且还得修改配置文件顶部,还不如在appSettings里新增一项来得方便,为了方便写appSettings节,后来在写配置文件的类里又新增一个写方法

       public void WriteAppSettings(string itemName,string itemValue)

       {

           XmlNode appSection = cfgSection.SelectSingleNode("//appSettings/add[@key='" + itemName + "']");

           if (appSection != null)

           {

              XmlAttribute valueAttribute = appSection.Attributes["value"];

              valueAttribute.Value = itemValue;

           }

       }

 

至此,有关系统配置的管理基础设施完成,接下来要做的只是做些可以用来设置数据库连接、设置管控端地址等变量的窗体,在这些窗体的代码你将可以用一个个的类来操作你的配置。优势更为明显的地方在于硬件配置、菜单、工具栏配置这些带有集合性质的地方,到时我们面对的不再是一串串的XML,而是一个个的list,操作起来极其的方便,大大减少程序实现中出现的一些低级错误。

转载于:https://www.cnblogs.com/lichdr/archive/2007/06/14/783543.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值