一:获取和设置默认配置文件
/// <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. LeetCode Find All Duplicates in an Array

    原题链接在这里:https://leetcode.com/problems/find-all-duplicates-in-an-array/ 题目: Given an array of integer ...

  2. linux 查找文件的命令

    http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html

  3. Netty NIO 框架性能压测-短链接-对比Tomcat

    压测方案 准备多个文件大小分别为 1k 10k 100k 300k 使用ab分别按 [50,2000](按50逐渐叠加)压测服务,每次请求10W次 硬件信息:CPU:Intel(R) Xeon(R) ...

  4. shell 除法 小数点

    比如: num1=2 num2=3 num3=`echo "scale=2; $num1/$num2" | bc` 使用bc工具,sclae控制小数点后保留几位 还有一种方法 aw ...

  5. sql语句注意事项

    1两表根据a字段关联,把t2表中的c字段值更新到t1表中的c字段update T1set T1.C =(select T2.C from T2 where T1.A = T2.A)where exis ...

  6. expdp ORA-31693 ORA-31617 ORA-19505 ORA-27037

    使用expdp并行导出数据的时候报如下错误: Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64b ...

  7. php7.0.12 laravel 链接sqlserver数据库

    https://www.microsoft.com/en-us/download/details.aspx?id=20098 下载最后一个,然后这个工具可以将dll扩展下载下来,选择一个空白的文件夹就 ...

  8. LeetCode: Linked List Random Node

    这题参照http://blog.jobbole.com/42550/ 用的蓄水池算法,即更改ans的概率为1/(当前length) /** * Definition for singly-linked ...

  9. Windows Phone 十、数据绑定

    数据绑定:是一种 XAML 和后台数据交互的方式(桥梁) 通过后台进行数据绑定 <Grid> <TextBox x:Name="txtHello" Text=&q ...

  10. js基础练习一之tab选项卡

    最近在学习前端,当然包括js,css,html什么的,在听课时做的一些小练习,记录下来: 实例一: --Tab选项卡-- <script type="text/javascript&q ...