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"; } }
下面为调用:
查看全文
相关阅读:
C/C++ volatile
Linux fopen/fread/fwrite和open/read/write
反思 承认失败,负重前行。
算法和数据结构 打印回形矩阵
Linux 软链接和硬链接
数据库 CAS原理和ABA问题
算法和数据结构 海量数据求前K个数
算法和数据结构 冒泡排序
java中的经典算法之选择排序(SelectionSort)
【Java】常量、关键字、标识符、注释
原文地址:https://www.cnblogs.com/PingleDay/p/3477995.html
最新文章
svn:权限设置(创建用户和用户组、权限设置)
svn:版本和冲突(查看版本信息、恢复到历史版本、解决冲突、图标集)
svn:客户端与服务器端(安装与简单使用)
小程序:WXML语法(数据绑定、列表渲染、条件渲染、九九乘法表、导航条)
小程序:组件(图片、图标、VIEW、进度条、父文本、按钮、开关、输入框、input)
js:变量的声明方式(const、var、let)
iconfont图标(图片格式、字体格式)
CSS:元素的显示与隐藏(display、visibility、overflow)
数据存储模式
内存
热门文章
寄存器
位运算之加减乘除
位运算
无符号数、有符号数、原码、反码、补码
基础知识——存储单位、数制、数据宽度
vue踩坑之路由跳转了,但是页面没有跳转。且报错Cannot read property '__ob__' of undefined
webpack之核心概念
ant-design-vue之FormModel表单自定义验证
C/C++ C++11 =default和=delete
Linux 内存泄漏分析
Copyright © 2011-2022 走看看