ExeConfigurationFileMap 这个类提供了修改、获取指定 config 的功能;
新建一个 ExeConfigurationFileMap 的实例 ecf ;
并设置 ExeConfigFilename 属性为要操作的 config 文件路径;
使用 ConfigurationManager.OpenMappedExeConfiguration 方法得到操对象 Configuration config
调用 Configuration 对象实例提供的 config.AppSettings.Settings[key].Value 可以修改或者获取 appsetting 的值了;
如果修改需要调用一下保存方法,config.Save();

/// <summary>
/// 获取自定义 index.config 文件中的 appsetting 节点值
/// </summary>
/// <param name="key">节点名称</param>
/// <returns></returns>
public static string GetIndexConfigValue(string key)
{
 string indexConfigPath = @"D:\indexConfig";
 if (indexConfigPath.IsNullOrEmpty())
  throw new Exception("请检查应用程序配置文件 appSettings 节点,是否存在 indexConfig 且 value 不为空的配置节!");
 if (!File.Exists(indexConfigPath))
  throw new Exception(string.Format("配置文件不存在:{0}", indexConfigPath));

ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();
 ecf.ExeConfigFilename = indexConfigPath;
 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);
 return config.AppSettings.Settings[key].Value;
}
/// <summary>
/// 设置自定义 index.config 文件中的 appsetting 节点值
/// </summary>
/// <param name="key">节点名称</param>
/// <returns></returns>
public static bool SetIndexConfigValue(string key,string value)
{
 string indexConfigPath = @"D:\indexConfig";
 if (indexConfigPath.IsNullOrEmpty())
  throw new Exception("请检查应用程序配置文件 appSettings 节点,是否存在 indexConfig 且 value 不为空的配置节!");
 if (!File.Exists(indexConfigPath))
  throw new Exception(string.Format("配置文件不存在:{0}", indexConfigPath));

ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();
 ecf.ExeConfigFilename = indexConfigPath;
 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);
 config.AppSettings.Settings[key].Value = value;
 config.Save();
 return true;
}

转载请保留:http://blog.csdn.NET/xxj_jing/article/details/7682565

获取或设置config节点值的更多相关文章

  1. CheckBoxList 获取与设置选中的值

    /// <summary> ///CheckBoxListHelper 的摘要说明 ///CheckBoxList获取与设置选中的值 /// </summary> public ...

  2. C#通过属性名字符串获取、设置对象属性值

    之前理工项目从这个博客找到了相对应的方法:C#通过属性名字符串获取.设置对象属性值 https://www.cnblogs.com/willingtolove/p/12198871.html

  3. js 获取和设置css3 属性值的实现方法

    众多周知 CSS3 增加了很多属性,在读写的时候就没有原先那么方便了. 如:<div style="left:100px"></div> 只考虑行间样式的话 ...

  4. OpenCV获取与设置像素点的值的几个方法

    Title: OpenCV OpenCV像素值的获取与设置 Fn 1 : 使用 Mat 中对矩阵元素的地址定位的知识 (参考博文:OpenCV中对Mat里面depth,dims,channels,st ...

  5. jQuery获取、设置title的值

    获取值:var t = $(document).attr('title'); 设置值:$(document).attr('title','value');

  6. java 通过反射获取和设置对象属性值

    public static Object parseDate(Object object){ SimpleDateFormat sdf = new SimpleDateFormat("yyy ...

  7. 用val()获取与设置input的值

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. c# 根据配置文件路径,设置和获取config文件 appSettings 节点值

    /// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...

  9. C# 如何获取自定义的config中节点的值,并修改节点的值

    现定义一个方法 DIYConfigHelper.cs using System; using System.Xml; using System.Configuration; using System. ...

随机推荐

  1. MySQL5.5安装图解

    MySQL5.5安装图解... ====================== 第一部分:去官网下载MySQL安装包... MySQL下载官网:https://dev.mysql.com/downloa ...

  2. jquery动画切换引擎插件 Velocity.js 学习01

    一.Velocity.js介绍 Velocity是一个jQuery插件,重新实现了$.animate() 来产生更高的性能(速度也比CSS动画库更快),而包括新的功能,以提高动画工作流程. Veloc ...

  3. ZooKeeper动态配置(十四)

    概述 在3.5.0发行之前,ZK的全体成员和所有其它的配置参数是静态加载的在启动的时候并且在运行的时候不可变.操作员诉诸于"滚动重启" - 一个手动密集和改变配置文件容易出错的方法 ...

  4. Nginx+Tomcat关于Session的管理

    前言 Nginx+Tomcat对Session的管理一直有了解,但是一直没有实际操作一遍,本文从最简单的安装启动开始,通过实例的方式循序渐进的介绍了几种管理session的方式. nginx安装配置 ...

  5. II8部署WCF服务出错

    环境:Windows 2012 R2 + IIS 8.0 + .NET 4.5 错误404.3 - Not Found: 控制面板->程序->启用或关闭Windows功能,如下图所示,将需 ...

  6. js 拖动滑块验证

    备注:拖动滑块时尽量平移,chrome浏览器上没有卡顿情况,但是搜狗极速模式和360极速模式都遇到了卡顿,拖不动情况,应是浏览器内部对事件响应速度导致吧. JS代码: ;(function ($,wi ...

  7. [uva11137]立方数之和·简单dp

    小水题再来一发 给定一个正整数n<=1e4,求将n写成若干个正整数立方和的方法数 典型的多阶段模型 f[i][j]表示当前用到1~i的数,累计和为j的方案数. #include<cstdi ...

  8. 【BZOJ4373】算术天才⑨与等差数列 [线段树]

    算术天才⑨与等差数列 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 算术天才⑨非常喜欢和等 ...

  9. CSS哪些样式属性可以继承

    不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...

  10. 查看服务器是否被DDOS攻击的方法

    伴随着现代互联网络快速发展,更加容易出现被攻击.尤其是ddos攻击已经不在是大网站需要关心的事情了.不少中小型企业,也在遭受ddos攻击.站长对ddos攻击不了解,所以网站被ddos攻击的时候,都不会 ...