现定义一个方法 DIYConfigHelper.cs

using System;
using System.Xml;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.IO; namespace Chain.Common
{ /// <summary>
/// Summary description for ReadWriteConfig.
/// </summary>
public class DIYConfigHelper
{
/// <summary>
///
/// </summary>
/// <param name="key">节点名称</param>
/// <returns></returns> /// <summary>
/// 获取自定义 index.config 文件中的 appsetting 节点值
/// flag -1:配置文件不存在 -2::节点不存在
/// </summary>
/// <param name="path">config文件的路径</param>
/// <param name="key">节点名称</param>
/// <returns>节点名称的值</returns>
public static string GetIndexConfigValue(string path, string key)
{
string flag = "";
string indexConfigPath = path;
if (string.IsNullOrEmpty(indexConfigPath))
return flag = "-1";//配置文件为空
if (!File.Exists(indexConfigPath))
return flag = "-1";//配置文件不存在 ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();
ecf.ExeConfigFilename = indexConfigPath;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);
try
{
flag = config.AppSettings.Settings[key].Value;
}
catch (Exception)
{
flag = "-2";
}
return flag;
} /// <summary>
/// 设置自定义 index.config 文件中的 appsetting 节点值
/// </summary>
/// <param name="path">config文件的路径</param>
/// <param name="key">节点名称</param>
/// <param name="value">需要修改的值</param>
/// <returns>true:修改成功 false:修改失败</returns>
public static bool SetIndexConfigValue(string path, string key, string value)
{
string indexConfigPath = path;
if (string.IsNullOrEmpty(indexConfigPath))
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;
} /// <summary>
/// 给xlm指定的节点添加节点和值
/// </summary>
/// <param name="path">xml文件的路径</param>
/// <param name="key">添加的key值</param>
/// <param name="value">添加的value值</param>
public static void AddIndexConfigValue(string path, string key, string value)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path); //加载xml文件
XmlNode rootXml = xmlDoc.SelectSingleNode("configuration"); //查询XML文件的根节点("siteMapPath")
XmlNodeList xnl = rootXml.SelectNodes("appSettings"); //获取所有节点为"siteMapNode"的节点
foreach (XmlNode xnItem in xnl)
{
XmlElement xe = (XmlElement)xnItem; //将子节点类型转换为XmlElement类型
XmlElement newXE = xmlDoc.CreateElement("add");
newXE.SetAttribute("key", key);
newXE.SetAttribute(@"value", value);
xnItem.AppendChild(newXE);
}
xmlDoc.Save(path);
}
/// <summary>
/// 按xml路径删除指定节点
/// </summary>
/// <param name="path">xml文件路径</param>
/// <param name="key">要删除的节点key值</param>
/// <returns>0:删除失败,1:删除成功,-1:配置文件异常,-2系统异常,</returns>
public static string DeleteIndexConfigValue(string path, string key)
{
string flag = "";
string indexConfigPath = path;
if (string.IsNullOrEmpty(indexConfigPath))
return flag = "-1";//配置文件为空
if (!File.Exists(indexConfigPath))
return flag = "-1";//配置文件不存在 ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();
ecf.ExeConfigFilename = indexConfigPath;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);
try
{
config.AppSettings.Settings.Remove(key);
config.Save(ConfigurationSaveMode.Modified);
flag = "";
}
catch (Exception)
{
flag = "-2";//系统异常
}
return flag;
}
}
}

调用方式:

    string ss = Chain.Common.DIYConfigHelper.GetIndexConfigValue(HttpRuntime.AppDomainAppPath.ToString() + "DIY.config", "");//获取节点值
bool tt = Chain.Common.DIYConfigHelper.SetIndexConfigValue(HttpRuntime.AppDomainAppPath.ToString() + "DIY.config", "", "");//修改节点值
Chain.Common.DIYConfigHelper.AddIndexConfigValue(HttpRuntime.AppDomainAppPath.ToString() + "DIY.config", "", "");//添加节点和值 string mm=Chain.Common.DIYConfigHelper.DeleteIndexConfigValue(HttpRuntime.AppDomainAppPath.ToString() + "DIY.config", "14");//删除指定值的节点
 

DIY.config文件的内容

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="" value="663CFB4AF7AE2A91B14587C31B3DE60AF38AED2E63F5040C5D453CBC704162B8ACDD7A7D67A95FA0" />
<add key="" value="156D7DB054ABBF9B321B1E8982130FDA3420475BC524C4259C55A8CEA4F884DE649FD16284A1053F" />
</appSettings>
<connectionStrings />
</configuration>

C# 如何获取自定义的config中节点的值,并修改节点的值的更多相关文章

  1. iOS开发小技巧--获取自定义的BarButtonItem中的自定义View的方法(customView)

    如果BarButtonItem是通过[[UIBarButtonItem alloc] initWithCustomView:(nonnull UIView *)]方法设置的.某些情况下需要修改BarB ...

  2. 从SuperSocket的App.config中读取配置,并修改保存,再重启服务

    string XmlPath = System.Windows.Forms.Application.ExecutablePath + ".config"; XmlDocument ...

  3. Odoo中的逆计算——由compute字段的值逆向修改其依赖值

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9281406.html 当通过compute属性指定方法,根据依赖值计算得到当前字段值时.一般也要制定这个计算的 ...

  4. 获取或设置config节点值

    ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...

  5. 用于获取或设置Web.config/*.exe.config中节点数据的辅助类

    1. 用于获取或设置Web.config/*.exe.config中节点数据的辅助类 /**//// <summary> /// 用于获取或设置Web.config/*.exe.confi ...

  6. [asp.net mvc 奇淫巧技] 01 - 封装上下文 - 在View中获取自定义的上下文

    我们在asp.net 开发中已经封装了最强大的HttpContext,我们可以在HttpContext中可以获取到几乎任何想获取的东西,也可以在HttpContext写入需要返回客户端的信息.但是这些 ...

  7. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  8. web.config中sessionState节点的配置方案

    web.config中sessionState节点的配置方案 web.config关于sessionState节点的配置方案,sessionState有五种模式:Custom,off,inProc,S ...

  9. web.config中<customErrors>节点

    错误提示: “/”应用程序中的服务器错误.------------------------------------------------------------------------------- ...

随机推荐

  1. 理解PHP中的会话控制

    会话控制是一种跟踪用户的通信方式,使用会话控制主要基于以下几点:由于http协议的无状态性,使得不能通过协议来建立两次请求之间的关联:对于通常的页面之间的数据传递方式get和post而言,主要处理参数 ...

  2. luogu P3233 [HNOI2014]世界树

    传送门 我是什么时候写的这题的qwq 首先,发现关键点的总数被限制了,很自然想到虚树,并且,对于一个关键点,他管理的点显然是一个联通块 然后把虚树先建出来,然后两次dfs,第一次是向祖先更新离每个点最 ...

  3. ubuntu 18.04 安装 Redis

    这篇博客写得不错,直接看这篇博客就OK了. https://wangxin1248.github.io/linux/2018/07/ubuntu18.04-install-redis.html

  4. Codeforces Round #494 (Div. 3) D. Coins and Queries(贪心

    题目链接 题目大意:给你n个物品,第iii个物品价值aia_iai​,询问q次,问你能不能凑出价值为qiq_iqi​的物品. 小贪心吧.从大到小找,能拿就拿就行了. #include<bits/ ...

  5. visual studio code运行时报错,无法将“cnpm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称,Cannot find module 'webpack'

    前言 因公司技术需求,这段时间成功进入了Vue 2.0 的坑,刚用起Visual Studio Code,却发现问题很多,发现一个错误:cnpm : 无法将“cnpm”项识别为 cmdlet.函数.脚 ...

  6. 前端下载excel打不开求助+解法

    解法: //axios   return request({ url: "/saleUnit/exportSaleUnit", headers: { "biz-sourc ...

  7. react踩坑记录——使用fetch获取json数据报错

    报错: 原因其实是list.json文件路径错误,该文件路径是相对于index.html的,而不是App.js或者index.js.

  8. 微信小程序获取手机验证码

    一种比较常见的功能获取手机验证码 先看效果图: 其实这个功能实现起来很简单,主要就是调取第三方接口,拿到返回值验证的问题 直接上代码吧: wxml页面: <view class='changeI ...

  9. [转] 如何轻松愉快地理解条件随机场(CRF)?

    原文链接:https://www.jianshu.com/p/55755fc649b1 如何轻松愉快地理解条件随机场(CRF)?   理解条件随机场最好的办法就是用一个现实的例子来说明它.但是目前中文 ...

  10. 在Linux环境下使用Apache部署ASP.NET Core

    在前几篇文章中我们一起探讨了如何在Linux环境中安装ASP.NET Core运行时环境及将ASP.NET Core项目部署在Jexus中,这篇文章中我们将探讨如何将ASP.NET Core部署于Ap ...