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. solr集群安装部署

    一.安装部署zookeeper集群 zookeeper集群 二.solr集群部署 集群配置 IP | 节点名称 | 环境 --- | --- | --- 192.168.137.128 | 192.1 ...

  2. (转)python编码问题

    时不时总是会碰到令人头疼的编码问题,这里推荐一篇决定好文,需反复诵读之: http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html . . ...

  3. nodejs formidable混合表单提交

    废话不多说,直接上代码: 前端页面: <!DOCTYPE html><html><head><link rel=’stylesheet’ href=’/sty ...

  4. mysql5.6以上(适用5.7)免安装版本 终极配置

    1.解压你的mysql5.6 我解压的位置是D:\Program Files\mysql--winx64,你可以随意放在任何位置,不建议解压到C盘 2.来到你解压的文件根目录下,新建一个my.ini文 ...

  5. ZooKeeper入门(四)

    入门:使用ZooKeeper的协调分布式应用 这个文档使你对ZooKeeper快速入门,它主要针对想尝试它的开发者.并且包含简单的单机的ZooKeeper服务的安装说明,一些验证是否运行的命令,和一个 ...

  6. 【转】 GRASP(通用职责分配软件模式)模式

    转自:http://www.cnblogs.com/sevenyuan/archive/2010/03/05/1678730.html 及:http://blog.csdn.net/lovelion ...

  7. Linux命令之pstree - 以树状图显示进程间的关系

    pstree命令以树状图显示进程间的关系(display a tree of processes).ps命令可以显示当前正在运行的那些进程的信息,但是对于它们之间的关系却显示得不够清晰.在Linux系 ...

  8. bzoj 1050 并查集

    先按边长排序,假设s与t连通,那么我们可以枚举s与t的路径中最短的一条边,通过类似与kruskal的方法找到s与t的路径在当前最小边权情况下尽量小的最大边权,用这个比值更新答案. 特别的,我们对于某一 ...

  9. base--AuditResult

    //参考base-4.0.2.jar public class AuditResult implements TimeReferable, Serializable //参考api-1.0.0.jar ...

  10. javascript性能

    1.js文件放在底部 js文件具有阻塞机制,放在头部,需要等待js下载解析完毕之后才能下载渲染页面,因此需要放在底部