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. HDU1814 2-sat 模板

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. java nio buffer读取数据乱码问题

    public static void main(String[] args) throws IOException { String charsetName = "GBK"; St ...

  3. liunx系统下安装mysql数据库5.7.13版本

    一:在/usr/local目录下解压安装包

  4. 前端PHP入门-001-为什么学习PHP?

    写在前面的话 可能不知道能坚持多久,现在的我喜欢纯文字的描述! 希望能坚持写完,也是对自己的一个鞭策! 总顾及别人,那谁来顾及你! 为什么学习PHP? PHP入门简单,学习入门易入手[呵呵,都这么说, ...

  5. CSS知识之 background-position 用法详细介绍

    一.语法 background-position : length || length background-position : position || position 二.取值 length   ...

  6. [LeetCode] 6. ZigZag Conversion ☆☆☆

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  7. lightoj 1007 - Mathematically Hard 欧拉函数应用

    题意:求[a,b]内所有与b互质个数的平方. 思路:简单的欧拉函数应用,由于T很大 先打表求前缀和 最后相减即可 初次接触欧拉函数 可以在素数筛选的写法上修改成欧拉函数.此外本题内存有限制 故直接计算 ...

  8. Ubuntu12.04 GIT安装和使用

    一.安装GIT和配置GIT 1.安装GIT apt-get install git 2.配置GIT ##配置用户信息 git config --global user.name "John ...

  9. Android中TextView设置字体

    最近项目中出现把字体设置成宋体,微软雅黑,黑体,楷体等的需求; 度娘发现Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使 ...

  10. windows 上启动appium

    import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecuteResultHandl ...