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"; } }
下面为调用:
查看全文
相关阅读:
Java中怎样由枚举常量的ordinal值获得枚举常量对象
深入了解泛型
艰苦的RAW格式数据恢复之旅
eclipse 代码清理 代码格式化 代码凝视
应用策略设计模式
JavaEE SSH集成框架(两) struts2 本地加载dtd文件,action组态
关闭表空间的热备份实例重新启动重现错误和解决
CF 148D. Bag of mice (可能性DP)
C++ Primer 学习笔记_45_STL实践与分析(19)--建筑常规算法
工作的几点思考
原文地址:https://www.cnblogs.com/PingleDay/p/3477995.html
最新文章
MLAPP——概率机器学习知识汇总
android控制控制的显示顺序
java多线程学习(一)
分解问题
2014第4周一
2014第3周日
2014第3周六升级win8.1
2014第3周五学习笔记
2014第3周四杂记
2014第3周三JS进阶书籍
热门文章
java中classPath和Xpath问题
2014第3周一
要做的几个实践
项目中Spring注入报错小结
【设计优化】-使用缓冲(Buffer)提高程序性能
递归和迭代的差别
hadoop调度程序时出现“Error opening job jar”错误
企业邮箱服务优化软件
Linux安装程序Anaconda分析
佛祖保佑 永无BUG 永不改动
Copyright © 2011-2022 走看看