转自: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配置文件的更多相关文章

  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. yii2-更改默认显示的通用主页

    在views/layouts/目录下新建一个login.php,然后SiteController中更新下面的方法 public function actionIndex() { $this->l ...

  2. suds调用webservice

    一.安装 pip install suds 二.日志 import logging logging.basicConfig(level=logging.INFO) logging.getLogger( ...

  3. Ant-style path patterns

    [转载]http://blog.itpub.net/29959940/viewspace-1385870/ Ant path 匹配原则路径匹配原则(Path Matching) Spring MVC中 ...

  4. mysql导入导出数据中文乱码解决方法小结

    linux系统中 linux默认的是utf8编码,而windows是gbk编码,所以会出现上面的乱码问题. 解决mysql导入导出数据乱码问题 首先要做的是要确定你导出数据的编码格式,使用mysqld ...

  5. 做完c语言作业的心得

    算是第一次自己接触c语言,并不是很深入的了解了,但也完成了第一次课的作业.在没有复制粘贴的情况下,8遍的简单编程让我记下了它基本的格式. 实验1.2.3.7都是基本的输入字,和课上的练习差不多,巩固最 ...

  6. 20150207读书笔记<深入理解计算机系统2-1>

    第二章 信息存储 (1)  多数计算机以一个字节作为最小可寻址的存储器单元. 机器级程序将存储器看成一个非常大的字节数组,称为虚拟存储器. 存储器的每个字节都由唯一的数字标识,称为它的地址. 所有可能 ...

  7. js 扩展Array支持remove方法

    /* * 方法:Array.remove(dx) 通过遍历,重构数组 * 功能:删除数组元素. * 参数:dx删除元素的下标. */ Array.prototype.remove = function ...

  8. Js获取后台集合List的值并操作html

    功能:将后台传到前端JSP的List中的float型数值转换为百分比显示 HTML代码: <s:iterator value="colorConfigList" status ...

  9. checkbox --jquery

    <input type="checkbox" value="2" class="ace ace-checkbox-2" id='ass ...

  10. Windows Store App 偏移特效

    通过前面讲解的内容,读者已经了解了如何在三维空间中使旋转对象绕指定的旋转中心旋转一定的角度.接下来在这个基础上进一步讲解如何对旋转对象进行平移.下面首先介绍一下用到的几个属性. q  LocalOff ...