c# 根据配置文件路径,设置和获取config文件 appSettings 节点值
一:获取和设置默认配置文件
/// <summary>
/// 获取编译后的主配置文件节点值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetConnectionStringsConfig(string key)
{
try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = @"DMSystem.exe.config" };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
return configuration.AppSettings.Settings[key].Value;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 设置编译后的主配置文件节点值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static void SetConnectionStringsConfig(string key, string value)
{
try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = @"DMSystem.exe.config" };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); bool isModified = false;
//检测是否已经存在该键
foreach (string tempKey in configuration.AppSettings.Settings.AllKeys)
{
if (tempKey == key)
{
isModified = true;
}
}
if (isModified)
{
configuration.AppSettings.Settings.Remove(key);
} configuration.AppSettings.Settings.Add(key, value);
configuration.Save(ConfigurationSaveMode.Modified);
//刷新
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception ex)
{
throw ex;
}
}
二、获取和设置指定配置文件
/// <summary>
/// 获取指定config文件中appSettings节点下key的value
/// </summary>
/// <param name="key">appSettings > add > key </param>
/// <param name="configPath">配置文件绝对路径</param>
/// <returns></returns>
public static string GetAppSettings(string key, string configPath)
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var strObj = configuration.AppSettings.Settings[key]; if (strObj == null)
{
throw new ArgumentException($"当前配置文件路径{configuration.FilePath}\n文件中appSettings节点下未配置名为:【{key}】的内容,请添加后再试");
}
return strObj.Value;
} /// <summary>
/// 修改指定config文件中appSettings节点下key的value
/// </summary>
/// <param name="name">appSettings > add > key </param>
/// <param name="configPath">配置文件绝对路径</param>
/// <returns></returns>
public static bool SetAppSettings(string key, string value, string configPath)
{
try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); if (configuration.AppSettings.Settings[key] != null)
configuration.AppSettings.Settings[key].Value = value;
else
configuration.AppSettings.Settings.Add(key, value);
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch
{
return false;
}
}
c# 根据配置文件路径,设置和获取config文件 appSettings 节点值的更多相关文章
- 获取 config文件的节点值
System.Configuration.ConfigurationManager.AppSettings["followTemplate"];
- 利用HttpWebRequest模拟表单提交 JQuery 的一个轻量级 Guid 字符串拓展插件. 轻量级Config文件AppSettings节点编辑帮助类
利用HttpWebRequest模拟表单提交 1 using System; 2 using System.Collections.Specialized; 3 using System.IO; ...
- 转载:轻量级Config文件AppSettings节点编辑帮助类
using System.Configuration; using System.Windows.Forms; namespace Allyn.Common { public class XmlHep ...
- 轻量级Config文件AppSettings节点编辑帮助类
using System.Configuration; using System.Windows.Forms; namespace Allyn.Common { public class XmlHep ...
- Web.config中appSettings节点值两种读取方法
<appSettings> <add key="ClientPort" value="5252"/> <add ...
- 对获取config文件的appSettings节点简单封装
转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html C#的开发中,无论你是winform开发还是w ...
- js设置、获取单值cookie和多值cookie
js设置.获取单值cookie和多值cookie,代码如下: var CookieUtil = (function () { var Cookie = function () { // 获取单值coo ...
- 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数
1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...
- jQuery设置和获取HTML、文本和值
jQuery设置和获取HTML.文本和值 按 Ctrl+C 复制代码 <script type="text/javascript"> //<![CDATA[ $( ...
随机推荐
- linq实现数组转符号分割的字符串(备忘)
fitemidstr = string.Join(",", detailpre1.Select(i => i.Key.ToString()).ToArray());
- Nginx location 匹配顺序整理
Nginx location模块整理 具体的Nginx安装就不在这里描述了,这里只是为了对location的描述 Nginx环境 a. 查看当前系统cat /etc/redhat-release [r ...
- 把Mongodb配置成windows服务
在mongodb/bin 下运行命令窗口需要配置日志和db路径,如下:mongod --logpath d:\mongo\logs\logfilename.log --logappend --dbpa ...
- Nodes “-1” are listed in ADOP_VALID_NODES table but not in FND_NODES table
While trying to apply patches to upgrade to 12.2.4, adop failed due to the below errors. Validating ...
- sdk命令
SDK命令 常用sdk命令 开启adb服务:adb start -server 关闭adb服务:adb kill -server 查看模拟器/真机:adb devices 安装/卸载/运行程序: 安装 ...
- 使用hbuilder编辑器实现移动app打包
作为一枚web前端来讲,需要了解的东西太多,需要学习的也太多了,那天因为公司需求,就研究了下移动网站打包成app的方法,这种东西好像是H5出来后就有推出的,因为性功能不行,就没怎么关注,但现在移动互联 ...
- 把汉字转换为html实体编码
背景:工作中需要把汉字转换为html实体编码实现方式:import org.apache.commons.lang.StringEscapeUtils;public static void main( ...
- iOS 开发:TCP三次握手连接
在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认: 第二次握 ...
- Android 开发基础及环境配置
2011年买了第一部安卓操作系统的手机,当时势头正盛的HTC不可思议(incredible),当时的想法就是想学习下智能手机开发,但是由于各种原因,客观上是公司的项目太忙了,忙于项目管理.团队建设.客 ...
- rdesktop的使用
整个地球都知道rdesktop,有了它,我们可以从Solaris或者Linux使用Windows,当然Windows要开启Windows Terminal Service.虽然也有基于GTK+的tsc ...