一:获取和设置默认配置文件
/// <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. SQL性能优化

    引言: 以前在面试的过程中,总有面试官问道:你做过sql性能优化吗?对此,我的答复是没有.一次没有不是自己的错误,两次也不是,但如果是多次呢?今天痛下决心,把有关sql性能优化的相关知识总结一下,以便 ...

  2. C#实现XML与DataTable互转

    private string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; XmlTextWriter wr ...

  3. nodejs 的ajax获取数据express

    var request = require('request'); request('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access ...

  4. CCNET+MSBuild+SVN实现每日构建

    最近开始将源代码迁移到SVN,于是便考虑到如何从SVN定期获取源码,自动编译并部署以减轻工作量并提高工作效率.通过多方搜集资料并进行研究,基本实现了这个功能.对于每日构建的概念就不具体展开了,可以在各 ...

  5. mysql 触发器示例和注解

    -- 格式 CREATE TRIGGER 触发器名称 AFTER|before insert|update|delete ON 触发表 FOR EACH ROW BEGIN insert into 处 ...

  6. 轻松三步教你配置Oracle—windows环境

    最近笔者在学习Oracle的时候,虽然度过了大家所说的安装难题,但是又遇到了一系列的问题,经过多方求教才知道原来是自己仅仅是安装了Oracle,却没有在环境变量中进行相应的配置.笔者也像大家遇到问题时 ...

  7. 在eclipse上开发nodejs

    首先到官网下载nodejs.地址:https://nodejs.org/en,可根据自己的操作系统选择下载. 安装好后.进入命令行输入node ,然后输入console.log("hello ...

  8. git操作笔记

    首先本文参考廖雪峰的git学习教程,写的非常好,值得学习. http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b80 ...

  9. asp.net 事件加载顺序

    下面是母版页与内容页合并后事件的发生顺序: 母版页控件 Init 事件. 内容控件 Init 事件. 母版页 Init 事件. 内容页 Init 事件. 内容页 Load 事件. 母版页 Load 事 ...

  10. ios 使用AFN上传图片到服务器

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSe ...