C#中动态读写App.config配置文件
转自:http://blog.csdn.net/taoyinzhou/article/details/1906996
app.config 修改后,如果使用cofnigurationManager立即读取,不能获取修改后的值,只能获取缓存中的原值。使用下面直接操场xml文件的方式可以动态读写配置文件。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Xml;
- namespace TaoNet.Common.TaoCommon
- {
- /// <summary>
- /// C#中动态读写App.config配置文件
- /// </summary>
- public class AppConfig
- {
- public AppConfig()
- {
- ///
- /// TODO: 在此处添加构造函数逻辑
- ///
- }
- /// <summary>
- /// 写操作
- /// </summary>
- /// <param name="strExecutablePath"></param>
- /// <param name="AppKey"></param>
- /// <param name="AppValue"></param>
- public static void ConfigSetValue(string strExecutablePath,string AppKey, string AppValue)
- {
- XmlDocument xDoc = new XmlDocument();
- //获取可执行文件的路径和名称
- xDoc.Load(strExecutablePath + ".config");
- XmlNode xNode;
- XmlElement xElem1;
- XmlElement xElem2;
- xNode = xDoc.SelectSingleNode("//connectionStrings");
- // xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
- xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@name='" + AppKey + "']");
- if (xElem1 != null) xElem1.SetAttribute("connectionString", AppValue);
- else
- {
- xElem2 = xDoc.CreateElement("add");
- xElem2.SetAttribute("name", AppKey);
- xElem2.SetAttribute("connectionString", AppValue);
- xNode.AppendChild(xElem2);
- }
- xDoc.Save(strExecutablePath + ".config");
- }
- /// <summary>
- /// 读操作
- /// </summary>
- /// <param name="strExecutablePath"></param>
- /// <param name="appKey"></param>
- /// <returns></returns>
- public string ConfigGetValue(string strExecutablePath, string appKey)
- {
- XmlDocument xDoc = new XmlDocument();
- try
- {
- xDoc.Load(strExecutablePath + ".config");
- XmlNode xNode;
- XmlElement xElem;
- xNode = xDoc.SelectSingleNode("//appSettings");
- xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
- if (xElem != null)
- return xElem.GetAttribute("value");
- else
- return "";
- }
- catch (Exception)
- {
- return "";
- }
- }
- }
- }
C#中动态读写App.config配置文件的更多相关文章
- 在WCF程序中动态修改app.config配置文件
今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google搜了一下,在园子里的文章动态修改App.Config 和w ...
- C# 读写App.config配置文件
一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...
- C# 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- WinForm读写App.config配置文件
一.配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...
- 【C#】#103 动态修改App.config配置文件
对 C/S模式 下的 App.config 配置文件的AppSetting节点,支持配置信息现改现用,并可以持久保存. 一. 先了解一下如何获取 配置信息里面的内容[获取配置信息推荐使用这个] 1.1 ...
- C# 读写App.config 配置文件
先要添加引用:System.Configuration //using System.Configuration; if (System.IO.File.Exists(AppDomain.Curren ...
- 修改 App.Config 配置文件 C#
[转]在WCF程序中动态修改app.config配置文件 今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google ...
- C# 读写App.config
Jul142013 [C#] 读写App.config配置文件的方法 作者:xieyc 发布:2013-07-14 17:29 字符数:3433 分类:编程 阅读: 39,139 次 ...
随机推荐
- C# 使用HttpWebRequest通过PHP接口 上传文件
1:上传文件实例 public void UploadXMLLog(string xmlpath) { NameValueCollection nvc = ne ...
- STM32学习笔记(六) SysTick系统时钟滴答实验(stm32中断入门)
系统时钟滴答实验很不难,我就在面简单说下,但其中涉及到了STM32最复杂也是以后用途最广的外设-NVIC,如果说RCC是实时性所必须考虑的部分,那么NVIC就是stm32功能性实现的基础,NVIC的难 ...
- JS Map 和 List 的简单实现代码
javascript中是没有map和list 结构的. 本篇文章是对在JS中Map和List的简单实现代码进行了详细的分析介绍,需要的朋友参考下 代码如下: /* * MAP对象,实现MAP功能 * ...
- iframe 根据加载内容调整高度
iframe标签代码 <iframe id="checkListFrame" name="checkListFrame" src="Ind ...
- &与&&的区别
&是“逻辑与”(“按位与”“位运算符”),一定要判断完所有的条件才能确定到底返回true还是false. &&是“短路与”(“逻辑运算符”),当从左至右判断时,一旦出现有一个条 ...
- win7文件夹共享(不新建账户、不带密码直接访问)
1.右键需要共享的文件夹>共享>特定用户>选择Everyone>“添加”>“共享” 2.打开控制面板>按类别的查看方式>网络和Internet下的选择家庭组和 ...
- 已知GBK的某段码表,码表对应的字符
for i in range(0xA1A2,0xA1A5): ...
- ios 程序学习
马上着手开发iOS应用程序:五.提交应用与寻找信息 2013-01-11 15:36 佚名 apple.com 我要评论(0) 字号:T | T 本文介绍了您已经学习完如何开发一个优秀的iOS应用之后 ...
- 在 Ubuntu 14.04 中配置 PXE 服务器
PXE(预启动执行环境Preboot Execution Environment)服务器允许用户从网络中启动 Linux 发行版并且可以不需要 Linux ISO 镜像就能同时在数百台 PC 中安装. ...
- java计算组合数
import java.util.Scanner; public class Zuheshu { //计算m阶乘 public static int Fun(int m){ int su ...