一:获取和设置默认配置文件
/// <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 节点值的更多相关文章

  1. 获取 config文件的节点值

    System.Configuration.ConfigurationManager.AppSettings["followTemplate"];

  2. 利用HttpWebRequest模拟表单提交 JQuery 的一个轻量级 Guid 字符串拓展插件. 轻量级Config文件AppSettings节点编辑帮助类

    利用HttpWebRequest模拟表单提交   1 using System; 2 using System.Collections.Specialized; 3 using System.IO; ...

  3. 转载:轻量级Config文件AppSettings节点编辑帮助类

    using System.Configuration; using System.Windows.Forms; namespace Allyn.Common { public class XmlHep ...

  4. 轻量级Config文件AppSettings节点编辑帮助类

    using System.Configuration; using System.Windows.Forms; namespace Allyn.Common { public class XmlHep ...

  5. Web.config中appSettings节点值两种读取方法

        <appSettings>    <add key="ClientPort" value="5252"/>   <add ...

  6. 对获取config文件的appSettings节点简单封装

    转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html C#的开发中,无论你是winform开发还是w ...

  7. js设置、获取单值cookie和多值cookie

    js设置.获取单值cookie和多值cookie,代码如下: var CookieUtil = (function () { var Cookie = function () { // 获取单值coo ...

  8. 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

    1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...

  9. jQuery设置和获取HTML、文本和值

    jQuery设置和获取HTML.文本和值 按 Ctrl+C 复制代码 <script type="text/javascript"> //<![CDATA[ $( ...

随机推荐

  1. 第四篇:白话tornado源码之褪去模板外衣的前戏

    加班程序员最辛苦,来张图醒醒脑吧! ... ... ... 好了,醒醒吧,回归现实看代码了!! 执行字符串表示的函数,并为该函数提供全局变量 本篇的内容从题目中就可以看出来,就是为之后剖析tornad ...

  2. DataAnnotations里DisplayAttribute和 ValidationAttribute 如何从资源文件读取信息,显示多语言

    这个是我们常见的AccountModel代码 public class ChangePasswordModel { [Required] [DataType(DataType.Password)] [ ...

  3. front-end plugin, generate pdf with html5 and jquery

    http://www.jqcool.net/jquery-jspdf.html[from this site] <html> <head></head> <s ...

  4. Rational Rose2007下载安装教程以及问题处理

    Rational Rose2007详细安装步骤 学习了UML,那么Rational rose画图软件当然就是必不可少的了.我的电脑是win7 64位的系统.下面的链接是安装软件以及破解方法.该软件是B ...

  5. WORD学习之制作座位表

    1.点击插入-表格-插入表格,将表格的列数设置为6,行数为10,再点击“确定”按钮

  6. swift基础:第六部分:类与对象

    http://reactnative.cn/docs/0.24/getting-started.html#content(react Native 开发文档) 互联网这个时代,你松懈一天,就会有很多很 ...

  7. VIM 常用快捷键

    一,光标移动 大家不要觉得光标移动不重要,其实它是基础,更好的光标移动,复制,粘贴,删除等才能更加的得心应手,进入了编辑器里面后,鼠标就不能用了. 光标移动 h 或 向左箭头键(←) 20h或者20( ...

  8. C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编

      验证电话号码的主要代码如下: public bool IsTelephone(string str_telephone) { return System.Text.RegularExpressio ...

  9. app加固

    为什么要加固APP? 答:因为黑客通过反编译APK得到源码后,会在应用中插入代码,获取利益,比如添加广告,盗取用户账号.密码,后台定制活动等.   反编译的方法? 反编译是指apk文件通过反编译工具( ...

  10. 自定义置顶TOP按钮

    简述一下,分为三个步骤: 1. 添加Html代码 2. 调整Css样式 3. 添加Jquery代码 具体代码如下: <style type="text/css"> #G ...