zoukankan
html css js c++ java
winform app.config文件的动态配置
获取
获取
应用程序exe.config文件中 节点value值
/// <summary> /// 功能: 读取应用程序exe.config文件中 /// appSettings节点下 节点add属性值 /// 根据add的属性值key来读取value值 /// </summary> /// <param name="appKey">属性key值</param> /// <returns></returns> public string GetConfigValue(string appKey) { XmlDocument xDoc = new XmlDocument(); try { //System.Windows.Forms.Application.ExecutablePath可执行文件路径(包括执行文件名称) //加载文件 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); XmlNode xNode; XmlElement xElem; //选取appSettings节点 xNode = xDoc.SelectSingleNode("//appSettings"); //根据key属性值appKey选择节点 xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']"); if (xElem != null) //返回value属性值 return xElem.GetAttribute("value"); else return ""; } catch (Exception) { return ""; } }
上面是方法:
下面为调用:
GetConfigValue("key值");
修改
上面是方法:
修改
应用程序exe.config文件及App.config中 节点value值
/// <summary> /// 功能:动态配置app.config /// </summary> /// <param name="AppKey">节点属性key值</param> /// <param name="AppValue">节点属性value值</param> private void SetValue(string AppKey, string AppValue) { for (int i = 0; i < 2; i++) { XmlDocument doc = new XmlDocument(); doc.Load(AppConfig(i)); XmlNode node = doc.SelectSingleNode(@"//appSettings"); XmlElement ele = (XmlElement)node.SelectSingleNode(@"//add[@key='" + AppKey + "']"); ele.SetAttribute("value", AppValue); doc.Save(AppConfig(i)); } } /// <summary> /// 功能:重置数据库connectionStrings连接字符串 /// 时间:2013年12月31日11:09:57 /// </summary> /// <param name="strKey">节点属性name值</param> /// <param name="strValue">节点属性connectionString值</param> /// <returns></returns> private void WriteXml(string strKey, string strValue) { //循环2次 //分别修改App.config及应用程序exe.config connectionStrings节点值 for (int i = 0; i < 2; i++) { XmlDocument doc = new XmlDocument(); doc.Load(AppConfig(i)); XmlNode node = doc.SelectSingleNode(@"//connectionStrings"); XmlElement ele = (XmlElement)node.SelectSingleNode(@"//add[@name='" + strKey + "']"); ele.SetAttribute("connectionString", strValue); doc.Save(AppConfig(i));//保存 } } /// <summary> /// 功能:获取配置文件路径 /// </summary> /// <param name="i"></param> /// <returns></returns> public string AppConfig(int i) { if (i == 0)//获取应用程序目录下App.config路径 { int intPos = Application.StartupPath.Trim().IndexOf("bin") - 1; string strDirectoryPath = System.IO.Path.Combine(Application.StartupPath.Substring(0, intPos), "App.config"); return strDirectoryPath; } else//获取应用程序exe目录 如:MultiThreadDemo.exe.config目录路径 { return System.Windows.Forms.Application.ExecutablePath + ".config"; } }
下面为调用:
查看全文
相关阅读:
html5的跨域处理
file表单提交异步模拟
Bigpipe :bigpipe的了解之2
javascript获取指定父元素
javascript淡入淡出的效果轮换转播
IE下,动态创建的iframe在异步提交时会跳转的问题的解决
javascriptIE不支持table的innerHTML解决方案
javascript删除元素所引起的 对于NodeList的理解
javascript淡入淡出的效果轮换转播后续
数据加载的bigpipe
原文地址:https://www.cnblogs.com/PingleDay/p/3477995.html
最新文章
2009年下半年软考时间安排
hoj 1067 又是Hanoi塔问题
程序规划方法漫谈(转载)
hoj 1071 最大矩阵连乘次数
鼠标右键添加word
hoj 1062 General Search
hoj 1072 活动安排问题
什么样的网站建设才合适企业需要网站需求分析(转载)
AC生涯
关于fckeditor的详细配置
热门文章
如何完全删除一个非空文件夹
asp.net中img底部出现空白
对webconfig中 sessionState 的研究
js 幻灯片效果(兼容netscape)
编程的方式改变母板页的属性,从而改变页面外观
flash as3 画虚线参考
把重设新闻中图片尺寸的代码贴出来了,ie8测试通过
关于FCKEditor编辑器标题图片功能的实现
还是关于url重写的研究
蓝桥杯基础练习 特殊回文数
Copyright © 2011-2022 走看看