获取或设置config节点值
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节点值的更多相关文章
- CheckBoxList 获取与设置选中的值
/// <summary> ///CheckBoxListHelper 的摘要说明 ///CheckBoxList获取与设置选中的值 /// </summary> public ...
- C#通过属性名字符串获取、设置对象属性值
之前理工项目从这个博客找到了相对应的方法:C#通过属性名字符串获取.设置对象属性值 https://www.cnblogs.com/willingtolove/p/12198871.html
- js 获取和设置css3 属性值的实现方法
众多周知 CSS3 增加了很多属性,在读写的时候就没有原先那么方便了. 如:<div style="left:100px"></div> 只考虑行间样式的话 ...
- OpenCV获取与设置像素点的值的几个方法
Title: OpenCV OpenCV像素值的获取与设置 Fn 1 : 使用 Mat 中对矩阵元素的地址定位的知识 (参考博文:OpenCV中对Mat里面depth,dims,channels,st ...
- jQuery获取、设置title的值
获取值:var t = $(document).attr('title'); 设置值:$(document).attr('title','value');
- java 通过反射获取和设置对象属性值
public static Object parseDate(Object object){ SimpleDateFormat sdf = new SimpleDateFormat("yyy ...
- 用val()获取与设置input的值
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- c# 根据配置文件路径,设置和获取config文件 appSettings 节点值
/// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...
- C# 如何获取自定义的config中节点的值,并修改节点的值
现定义一个方法 DIYConfigHelper.cs using System; using System.Xml; using System.Configuration; using System. ...
随机推荐
- [洛谷P4774] [NOI2018]屠龙勇士
洛谷题目链接:[NOI2018]屠龙勇士 因为markdown复制过来有点炸格式,所以看题目请戳上面. 题解: 因为杀死一条龙的条件是在攻击\(x\)次,龙恢复\(y\)次血量\((y\in N^{* ...
- Flash Sort
FlashSort依然类似桶排,主要改进了对要使用的桶的预测,或者说,减少了无用桶的数量从而节省了空间,例如 待排数字[ 6 2 4 1 5 9 100 ]桶排需要100个桶,而flash sort则 ...
- bzoj 1036: [ZJOI2008]树的统计Count——树链剖分
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- node启动服务
npm install http-server -g http-server ipconfig查看当前ip 手机可访问第一个网址.
- 【转】cve-2013-2094 perf_event_open 漏洞分析
cve-2013-2094是于2013年4月前后发现的linux kernel本地漏洞,该漏洞影响3.8.9之前开启了PERF_EVENT的linux系统.利用该漏洞,通过perf_event_ope ...
- 【2017 Multi-University Training Contest - Team 1】小结
啊人生第一次打多校被虐 紧随yql的脚步做题. 1001: 可以发现我们平时表示的数都是$x*log_{10}{10}$,所以类似于做一个换底公式就可以了. -1是一个烟雾弹,因为小学生都知道2^n不 ...
- 【LA3882】And then there was one
做sb题也是一种乐趣,是吧…… #include<bits/stdc++.h> #define N 10005 using namespace std; int f[N],m,n,k; i ...
- Pylot网站Web服务器性能和负载压力测试-适用Windows可绘制图表
为了能够准确地评估网站服务器对网络流量的承受能力,我们一般会采取模拟网站用户访问,通过不断地增加并发数,延长访问时长,从而最终得出网站Web服务器的性能和负载能力.当然也可以通过Web压力测试,来完善 ...
- HA集群
//硬件准备: .两个机器,相同系统 .网卡ip为:aming 192.168.11.24 aming1 192.168.11.23 //实验准备: . hostname : aming , amin ...
- DOM编程艺术读书笔记 (须熟读)
http://www.qdfuns.com/notes/39151/00d8bc6322359f00450f492ae56bf69e.html