现定义一个方法 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. luogu 2371 墨墨的等式

    1.背包dp #include<bits/stdc++.h> #define rep(i,x,y) for(register int i=x;i<=y;i++) #define ll ...

  2. mysql日期时间函数(常用的)

    mysql> SELECT NOW();  #返回(打印)当前日期和时间+---------------------+| NOW() |+---------------------+| 2017 ...

  3. 关于PHP建立数据库访问类的封装以及操作php单例模式连接数据库封装类

    建立数据库访问类的封装 <?php   class DBDA {     public $host = "localhost"; //服务器地址     public $ui ...

  4. “无法找到XXX.exe的调试信息,或调试信息不匹配”解决方案

    错误信息如下: 解决方法: 选择项目属性,依次序进行如下操作. 1.选择 配置属性->链接器->调试->生成调试信息 改为 是 一般问题都是出现在这个地方,修改完了可以尝试运行,若还 ...

  5. pyqt5-控件是否可用

    setEnabled(bool)      设置控件是否可用 True  可用 isEnabled()      获取控件是否可用 s=button.isEnabled()

  6. 博客里的第一篇随笔!QWQ

    这里是一个信息蒟蒻,开始自己的博客之旅!!QWQQQQ

  7. python加密(MD5)

    # import hashlib # # 1. 创建一个MD5对象 # obj = hashlib.md5(b"flkjsdalkfjklasdjfklasjkflasdjklfasdjfl ...

  8. java ArrayList、Vector、LinkedList区别

  9. MySql DDL语言(数据库和数据表的管理)

    数据定义语言,负责数据库和数据表的管理 ⒈数据库的管理 1.创建数据库 create database if not exists DatabaseName; #if not exists可以省略 2 ...

  10. C++写文件

    头文件 ofstream -- 向文件写内容 实现代码 #include <vector> #include <string> #include <fstream> ...