wince xml 配置文件设置,直接上源码

 public static class ConfigManager
    {
        private static string configName="app.xml";
        private static string XMLPath = "";
        private static XmlDocument xmlIAUConfig;
        public static XmlDocument XMLIAUConfig
        {
            get { return xmlIAUConfig; }
            set { xmlIAUConfig = value; }
        }
        private static string platForm;
        private static string PlatForm
        {
            get
            {
                return Environment.OSVersion.Platform.ToString();
            }
        }
        private static string currentPath;
        public static string CurrentPath
        {
            get
            {
                if (PlatForm.Equals("WinCE"))
                {
                    currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                }
                else if (PlatForm.Equals("Win32NT"))
                {
                    currentPath = Directory.GetCurrentDirectory();
                }

                return currentPath;
            }
        }
       
        static ConfigManager()
        {
            xmlIAUConfig = new XmlDocument();
            XMLPath = CurrentPath;
            Int32 i = XMLPath.LastIndexOf("/");
            if (i >= 0)
                XMLPath = XMLPath.Remove(i);
            XMLPath = XMLPath + "//"+configName;
            xmlIAUConfig.Load(XMLPath);
        }

        public static String GetValue(String key)
        {
            xmlIAUConfig.Load(XMLPath);
            String value;
            String path = @"//DataAccess/appSettings/add[@key='" + key + "']";
            XmlNodeList xmlAdds = xmlIAUConfig.SelectNodes(path);

            if (xmlAdds.Count == 1)
            {
                XmlElement xmlAdd = (XmlElement)xmlAdds[0];

                value = xmlAdd.GetAttribute("value");
            }
            else
            {
                throw new Exception("IAUConfig配置信息设置错误:键值为" + key + "的元素不等于1");
            }

            return value;
        }
        public static void SavaConfig(string strKey, string strValue)
        {
            XmlDocument XMLDoc = new XmlDocument();
            XMLDoc.Load(configName);
            XmlNodeList list = XMLDoc.GetElementsByTagName("add");

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Attributes[0].Value == strKey)
                {
                    list[i].Attributes[1].Value = strValue;
                }
            }
            StreamWriter swriter = new StreamWriter(configName);
            XmlTextWriter xw = new XmlTextWriter(swriter);
            xw.Formatting = Formatting.Indented;
            XMLDoc.WriteTo(xw);
            xw.Close();
            swriter.Close();
        }
    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值