获取或设置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. ...
随机推荐
- HDU3524 数论
Perfect Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- mysql5.6以上(适用5.7)免安装版本 终极配置
1.解压你的mysql5.6 我解压的位置是D:\Program Files\mysql--winx64,你可以随意放在任何位置,不建议解压到C盘 2.来到你解压的文件根目录下,新建一个my.ini文 ...
- Debug模式下加载文件,运行程序异常的慢
今天在进行单元测试的时候,debug模式下加载速度很慢,但是run模式下速度很快. 原因:在debug模式下,断点位置不当,解决办法 移除编译器中的所有断点.
- 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...
- Go语言的并发和并行
不知道你有没有注意到,这段代码如果我跑在两个goroutines里面的话: package main import ( "fmt" ) func loop(done chan bo ...
- redis的安装和常用命令
一.redis的安装 1.windows安装redis 下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这 ...
- 暑假集训——cf热身赛部分题有感加其题解
刚刚开始集训,集训队队长暂时还没有拉专题,而是拉了部分codeforces上过题人数在2000左右的题组成了一场热身赛(其实就是一场练习),花了一天时间终于把它刷完了,其中很多题让我学到了很多骚操作, ...
- Yii2实现读写分离(MySQL主从数据库)
读写分离(Read/Write Splitting). 1.原理: 让主数据库(master)处理事务性增.改.删操作(INSERT.UPDATE.DELETE),而从数据库(slave)处理SELE ...
- java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 找不到jar包的问题,路径问题
1.参考连接: https://blog.csdn.net/huangbiao86/article/details/6428608 折腾了一上午,找到了这错误的原因.哎……悲剧! 确认包已经被导入we ...
- js 数组&字符串 去重
Array.prototype.unique1 = function() { var n = []; //一个新的临时数组 for(var i = 0; i < this.length; i++ ...