转自: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 "";
            }
        }
    }
}

最新文章

  1. (转)python requests的安装与简单运用
  2. 注释声明:TODO HACK XXX FIXME REVIEW
  3. zend create project prepare
  4. Java命令
  5. am335x UART1输入u-boot 调试信息代码修改
  6. PHP如何实现文件上传
  7. linux 启动模式
  8. xtjh
  9. gridview--基本的gridview
  10. android Loger日志类(获取内置sd卡)
  11. Java权限讲解
  12. PHP获取用户访问IP地址的5种方法
  13. call()与apply()传参需要注意的一点
  14. MULE-ET0 、 ET1、ET2、PT1、PT2
  15. CCNA网络工程师学习进程(9)GNS3的安装与配置
  16. input输入框控制字数
  17. 解决ASP.NET MVC 检测到有潜在危险的 Request.Form 值
  18. CPU火焰图
  19. leetcode 编译问题:Line x: member access within null pointer of type &#39;struct TreeNode&#39;
  20. python全栈开发笔记---------数据类型****整理****

热门文章

  1. 前端开发流程之(线上)绝对地址(图片+css+js)
  2. Java线程中yield与join方法的区别
  3. http://stackoverflow.com/questions/6065169/requestanimationframe-with-this-keyword
  4. 【转】CentOS 6.5安装pyspider过程记录
  5. linux下使用shell查看apache IP访问量
  6. WCF初探-7:WCF服务配置工具使用
  7. Codeforces 451E Devu and Flowers(组合计数)
  8. Windows 通用应用尝试开发 “51单片机汇编”第二次更新总结
  9. [CQOI 2014] 数三角形 &amp; 机械排序臂
  10. html a 链接标签title属性换行鼠标悬停提示内容的换行效果