在WindowsCE 中实现 ConfigurationManager 类

                                                          在WindowsCE 中实现 ConfigurationManager 类

 

       在Windows 下的移动开发中,并没有提供对配置文件的操作类,即没有提供 ConfigurationManager 类。

       可是在开发中,却经常需要用到配置文件,需要相关的类库支持。既然微软没有提供,那只好自己动手了。

       在VS2008中新建类库项目,命名为:SmartDeviceCommons,新建 ConfigurationManager 类。详细代码如下:

 

 using System; using System.Collections.Generic; using System.Text; using System.Data.Common; using System.Reflection; using System.Xml; using System.IO; using System.Collections; namespace SmartDeviceCommons { /// <summary> /// 读取配置文件的 ConfigurationManager类 /// qingliangwu /// wuqingliangwork@163.com /// 2009-10-9 /// </summary> public class ConfigurationManager { //实现 AppSettings 节点的读取 public static Hashtable AppSettings { get { return GetNameAndValue("appSettings", "key", "value"); } } //实现 ConnectionStrings 节点的读取 public static Hashtable ConnectionStrings { get { return GetNameAndValue("connectionStrings", "name", "connectionString"); } } /// <summary> /// 根据节点名,子节点名,获取指定值 /// </summary> /// <param name="sectionTag"></param> /// <param name="KeyOrName"></param> /// <param name="valueOrConnectionString"></param> /// <returns></returns> private static Hashtable GetNameAndValue(string sectionTag, string KeyOrName, string valueOrConnectionString) { Hashtable settings = new Hashtable(5);//初始化Hashtable //string assemblyPath = Assembly.GetCallingAssembly().GetName().CodeBase; string assemblyPath = Assembly.GetExecutingAssembly().GetName().CodeBase;//获取项目根路径,获取到得路径为://Program Files//setup//SmartDeviceWindowCE//SmartDeviceWindowCE.exe ////string configUrl = "//Program Files//setup//SmartDeviceWindowCE//SmartDeviceWindowCE.exe.config"; string configUrl = assemblyPath + ".config"; //添加 config 后缀,得到config 文件路径 XmlDocument cfgDoc = new XmlDocument(); FileStream fs; try { fs = new FileStream(configUrl, FileMode.Open, FileAccess.Read); } catch (FileNotFoundException es) { throw es; } cfgDoc.Load(new XmlTextReader(fs)); fs.Close(); XmlNodeList nodes = cfgDoc.GetElementsByTagName(sectionTag); foreach (XmlNode node in nodes) { foreach (XmlNode childNode in node.ChildNodes) { XmlAttributeCollection attributes = childNode.Attributes; settings.Add(attributes[KeyOrName].Value, attributes[valueOrConnectionString].Value); } } return settings; } } }

 

 在项目中添加文件名为SmartDeviceCommons.dll.config 的配置文件。 这样,就可以使用和ConfigurationManager 类一样的功能了。但在此处值实现了 AppSettings 和 ConnectionStrings 节点。

      在微软的 Mobile Client Software Factory 中有更完美的实现。下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyId=F9176708-9F57-4C0F-97FB-F9C65A9BBF22&displaylang=en

 

  此代码可以在 WindowsCE,pocketPC,smart phone 中使用。

  

转载于:https://www.cnblogs.com/wuqingliang/archive/2009/10/09/2555186.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值