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 次 ...
随机推荐
- iOS 推送通知处理
//这是程序杀死后再通过点击通知进入时调用的方法 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...
- 某篇ctr预估ppt的链接
csdn上面有一篇ppt,但是下载分太贵了.里面东西看起来讲的还可以.看看能不能嵌入. http://download.csdn.net/detail/u012289698/9371461 <i ...
- 0060 Linux SELinux 管理命令
1. SELinux 的起源 SELinux 是一个面向政府和行业的产品,由 NSA.Network Associates.Tresys 以及其他组织设计和开发.尽管 NSA 将其作为一个补丁集引入, ...
- laravel开发微信公众号1 之基本配置
需要用到的packagist: https://github.com/overtrue/laravel-wechat ( 目前最优雅的laravel微信sdk) 首先安装 compose ...
- python中列表的常用方法
s=[1,2,3] s[3]=12#列表长度小于3时无法给列表赋值 len(s)#列表长 s+s s*5#l列表重复5次 5 in s#判断元素是否在列表中,返回true or false max(s ...
- Android开发--LinearLayout的应用
1.简介 LinearLayout为安卓三大常用布局中的线性布局.其中,线性布局又分为水平线性布局和垂直线性布局.视图如下所示:
- xcodebuild
xcodebuild -workspace /path/union/moon-ios/Moon.xcworkspace -scheme Moon ONLY_ACTIVE_ARCH=NO TARGETE ...
- MYSQL 基于GTID的复制
1.概述 从MYSQL5.6 开始,mysql开始支持GTID复制. 基于日志点复制的缺点: 从那个二进制日志的偏移量进行增量同步,如果指定错误会造成遗漏或者重复,导致数据不一致. 基于GTID复制: ...
- centos 非可视化查看已安装的redis
1. 2.
- poj 2777
题意:两个操作:c l r x l到r之间的颜色变成x q l r 询问l到r有多少种颜色 思路:记一个整数表示哪种颜色是否取了 这里真的是煞笔了,看到这一题第一直觉是异或,但是A^A= ...