转自:http://blog.csdn.net/taoyinzhou/article/details/1906996

app.config 修改后,如果使用cofnigurationManager立即读取,不能获取修改后的值,只能获取缓存中的原值。使用下面直接操场xml文件的方式可以动态读写配置文件。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Xml;
  6.  
  7. namespace TaoNet.Common.TaoCommon
  8. {
  9. /// <summary>
  10. /// C#中动态读写App.config配置文件
  11. /// </summary>
  12. public class AppConfig
  13. {
  14. public AppConfig()
  15. {
  16. ///
  17. /// TODO: 在此处添加构造函数逻辑
  18. ///
  19. }
  20. /// <summary>
  21. /// 写操作
  22. /// </summary>
  23. /// <param name="strExecutablePath"></param>
  24. /// <param name="AppKey"></param>
  25. /// <param name="AppValue"></param>
  26. public static void ConfigSetValue(string strExecutablePath,string AppKey, string AppValue)
  27. {
  28. XmlDocument xDoc = new XmlDocument();
  29. //获取可执行文件的路径和名称
  30. xDoc.Load(strExecutablePath + ".config");
  31.  
  32. XmlNode xNode;
  33. XmlElement xElem1;
  34. XmlElement xElem2;
  35. xNode = xDoc.SelectSingleNode("//connectionStrings");
  36. // xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
  37. xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@name='" + AppKey + "']");
  38. if (xElem1 != null) xElem1.SetAttribute("connectionString", AppValue);
  39. else
  40. {
  41. xElem2 = xDoc.CreateElement("add");
  42. xElem2.SetAttribute("name", AppKey);
  43. xElem2.SetAttribute("connectionString", AppValue);
  44. xNode.AppendChild(xElem2);
  45. }
  46. xDoc.Save(strExecutablePath + ".config");
  47. }
  48.  
  49. /// <summary>
  50. /// 读操作
  51. /// </summary>
  52. /// <param name="strExecutablePath"></param>
  53. /// <param name="appKey"></param>
  54. /// <returns></returns>
  55. public string ConfigGetValue(string strExecutablePath, string appKey)
  56. {
  57. XmlDocument xDoc = new XmlDocument();
  58. try
  59. {
  60. xDoc.Load(strExecutablePath + ".config");
  61.  
  62. XmlNode xNode;
  63. XmlElement xElem;
  64. xNode = xDoc.SelectSingleNode("//appSettings");
  65. xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
  66. if (xElem != null)
  67. return xElem.GetAttribute("value");
  68. else
  69. return "";
  70. }
  71. catch (Exception)
  72. {
  73. return "";
  74. }
  75. }
  76. }
  77. }

C#中动态读写App.config配置文件的更多相关文章

  1. 在WCF程序中动态修改app.config配置文件

    今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google搜了一下,在园子里的文章动态修改App.Config 和w ...

  2. C# 读写App.config配置文件

    一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...

  3. C# 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  4. 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  5. WinForm读写App.config配置文件

    一.配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...

  6. 【C#】#103 动态修改App.config配置文件

    对 C/S模式 下的 App.config 配置文件的AppSetting节点,支持配置信息现改现用,并可以持久保存. 一. 先了解一下如何获取 配置信息里面的内容[获取配置信息推荐使用这个] 1.1 ...

  7. C# 读写App.config 配置文件

    先要添加引用:System.Configuration //using System.Configuration; if (System.IO.File.Exists(AppDomain.Curren ...

  8. 修改 App.Config 配置文件 C#

    [转]在WCF程序中动态修改app.config配置文件 今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google ...

  9. C# 读写App.config

    Jul142013 [C#] 读写App.config配置文件的方法 作者:xieyc   发布:2013-07-14 17:29   字符数:3433   分类:编程   阅读: 39,139 次 ...

随机推荐

  1. C# 使用HttpWebRequest通过PHP接口 上传文件

    1:上传文件实例 public void UploadXMLLog(string xmlpath)         {             NameValueCollection nvc = ne ...

  2. STM32学习笔记(六) SysTick系统时钟滴答实验(stm32中断入门)

    系统时钟滴答实验很不难,我就在面简单说下,但其中涉及到了STM32最复杂也是以后用途最广的外设-NVIC,如果说RCC是实时性所必须考虑的部分,那么NVIC就是stm32功能性实现的基础,NVIC的难 ...

  3. JS Map 和 List 的简单实现代码

    javascript中是没有map和list 结构的. 本篇文章是对在JS中Map和List的简单实现代码进行了详细的分析介绍,需要的朋友参考下 代码如下: /* * MAP对象,实现MAP功能 *  ...

  4. iframe 根据加载内容调整高度

    iframe标签代码 <iframe id="checkListFrame" name="checkListFrame"    src="Ind ...

  5. &与&&的区别

    &是“逻辑与”(“按位与”“位运算符”),一定要判断完所有的条件才能确定到底返回true还是false. &&是“短路与”(“逻辑运算符”),当从左至右判断时,一旦出现有一个条 ...

  6. win7文件夹共享(不新建账户、不带密码直接访问)

    1.右键需要共享的文件夹>共享>特定用户>选择Everyone>“添加”>“共享” 2.打开控制面板>按类别的查看方式>网络和Internet下的选择家庭组和 ...

  7. 已知GBK的某段码表,码表对应的字符

    for i in range(0xA1A2,0xA1A5):                                                                       ...

  8. ios 程序学习

    马上着手开发iOS应用程序:五.提交应用与寻找信息 2013-01-11 15:36 佚名 apple.com 我要评论(0) 字号:T | T 本文介绍了您已经学习完如何开发一个优秀的iOS应用之后 ...

  9. 在 Ubuntu 14.04 中配置 PXE 服务器

    PXE(预启动执行环境Preboot Execution Environment)服务器允许用户从网络中启动 Linux 发行版并且可以不需要 Linux ISO 镜像就能同时在数百台 PC 中安装. ...

  10. java计算组合数

    import java.util.Scanner; public class Zuheshu { //计算m阶乘    public static int Fun(int m){     int su ...