更改App.config里的值并保存
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace HandPickCrawlerDB.Extensions
{
public class AppHelper
{
private static string _appconfig = null;
public static string AppConfig
{
get
{
if (_appconfig == null)
{
Type t = typeof(System.Configuration.ConfigurationManager).Assembly.GetType("System.Configuration.ClientConfigurationHost");
object cfghst = Activator.CreateInstance(t, true);
PropertyInfo pi = t.GetProperty("ConfigPaths", BindingFlags.Instance | BindingFlags.NonPublic);
object cfgpath = pi.GetValue(cfghst, null);
Type t1 = typeof(System.Configuration.ConfigurationManager).Assembly.GetType("System.Configuration.ClientConfigPaths");
pi = t1.GetProperty("ApplicationConfigUri", BindingFlags.Instance | BindingFlags.NonPublic);
string path = (string)pi.GetValue(cfgpath, null);
if (string.IsNullOrEmpty(path))
_appconfig = string.Empty;
else
_appconfig = path.Replace(".vshost.", ".");
}
return _appconfig;
}
set
{
_appconfig = value;
}
}
public static void SetSettingToAppConfig(string key, string value)
{
if (string.IsNullOrEmpty(key))
{
throw new Exception("key not be null");
}
else
{
key = key.Trim();
}
if (string.IsNullOrEmpty(value))
value = "";
else
value = value.Trim();
if (!File.Exists(AppConfig))
{
throw new DirectoryNotFoundException();
}
File.SetAttributes(AppConfig, FileAttributes.Normal);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(AppConfig);
XmlNodeList xmllst = xmldoc.SelectNodes("/configuration/appSettings/add");
if (xmldoc.SelectSingleNode("/configuration/appSettings") == null)
{
XmlNode n2 = xmldoc.CreateNode("element", "appSettings", "");
n2.InnerXml = "<add key=\"" + key + "\" value=\"" + value + "\"/>";
xmldoc.SelectSingleNode("/configuration").AppendChild(n2);
xmldoc.Save(AppConfig);
}
else if (xmllst.Count == 0)
{
XmlNode n2 = xmldoc.CreateNode("element", "add", "");
XmlAttribute xa = xmldoc.CreateAttribute("key");
xa.Value = key;
n2.Attributes.Append(xa);
xa = xmldoc.CreateAttribute("value");
xa.Value = value;
n2.Attributes.Append(xa);
xmldoc.SelectSingleNode("/configuration/appSettings").AppendChild(n2);
xmldoc.Save(AppConfig);
}
else
{
bool existed = false;
foreach (XmlNode n1 in xmllst)
{
if (n1.Attributes["key"].Value.ToUpper() == key.ToUpper())
{
n1.Attributes["value"].Value = value;
xmldoc.Save(AppConfig);
existed = true;
break;
}
}
if (!existed)
{
XmlNode xmlnd = xmldoc.SelectSingleNode("/configuration/appSettings");
XmlNode n2 = xmldoc.CreateNode("element", "add", "");
XmlAttribute xa = xmldoc.CreateAttribute("key");
xa.Value = key;
n2.Attributes.Append(xa);
xa = xmldoc.CreateAttribute("value");
xa.Value = value;
n2.Attributes.Append(xa);
xmlnd.AppendChild(n2);
xmldoc.Save(AppConfig);
}
}
ConfigurationManager.RefreshSection("appSettings");
}
}
}
更改App.config里的值并保存的更多相关文章
- winform程序读取和改写配置文件App.config元素的值
winform程序读取和改写配置文件App.config元素的值 2016-05-16 17:49 by newbirth, 2412 阅读, 0 评论, 收藏, 编辑 1 2 3 4 5 6 7 & ...
- .net里面<app.config>中value值不能填写特殊符号问题
配置app.config或web.config的时候,经常要填写value值, 但是value值不能包含特殊字符,需要转义, 分享一下转义字符 App.config 实际上是 xml 文件,在标准 x ...
- c# 修改winform中app.config的配置值
public bool ChangeConfig(string AppKey,string AppValue) { bool result = true; try { XmlDocument xDoc ...
- 如何修改Web.Config里面的值
0.先添加 <add key="MAXNUM" value="6" /> 1.读取值 string maxNum = ConfigurationMa ...
- app.config中的值获取及设置 以及对log4net配置
修改或新增AppSetting节点 /// <summary> /// 修改AppSettings中配置 /// </summary> /// <param name ...
- c#配置文件app.config 与 Settings.settings
本篇博客将介绍C#中Settings的使用.参考:https://docs.microsoft.com/zh-cn/visualstudio/ide/managing-application-sett ...
- 【flask】flask项目配置 app.config
[理论] 在很多情况下,你需要设置程序的某些行为,这时你就需要使用配置变量.在Flask中,配置变量就是一些大写形式的Python变量, 你也可以称之为配置参数或配置键.使用统一的配置变量可以避免在程 ...
- C#的配置文件App.config使用总结 - 转
http://blog.csdn.net/celte/article/details/9749389 首先,先说明,我使用的app.config 配置文件的格式如下: <?xml version ...
- app.config应该放哪?
一:做了一个简单的三层构架的小例子,在主项目里调用工具类的方法实现在数据库里添加一条信息.先看下错误的提示信息是什么样的,如下图一,图二是调用工具类.直接在工具类里写上连接字符串就没问题,如果写到ap ...
随机推荐
- 面试中的一些小问题之ES5和ES6的区别?
1995年,JavaScript作为网景浏览器的一部分首次发布,起初并不叫JavaScript,而是叫LiveScript,但是因为当时Java正火,也算是为了搭上java的顺风车,于是改成了Java ...
- 【Python-2.7】换行符和制表符
在Python中换行符“\n”表示接下来的内容将会换到下一行显示,制表符“\t”表示下面的内容显示时在前面留出空白,如打印如下内容: Dear: I love you forever! 上面的一段话分 ...
- apache自带的ab压力测试工具
httpd-2.4.27-Win64-VC15 链接: https://pan.baidu.com/s/1027MtVwbq1zjUgF7P7Rrkw 密码: ne6a 下载解压后doc窗口cd .. ...
- MFC_2.1使用单选和多选框
使用单选和多选框 单选 1.拖控件 设置名字,CTRL+D设置顺序,属性设置第一个GROUP为TRUE: 2.设置第一个按钮绑定变量为 值 INT型 名称m_RadioIndxe; 3.设置单击响应内 ...
- CAD得到范围内实体(网页版)
主要用到函数说明: IMxDrawSelectionSet::Select 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARIA ...
- ThinkPHP---layer插件
[概论] (1)layer是基于jquery开发的一款美化弹框的插件,主要用于弹框效果的交互.但其他功能和组件也日益完善 官网:http://layer.layui.com 在线手册:http://w ...
- 剑指offer---正则表达式匹配
题目:正则表达式匹配 要求:请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所 ...
- <MyBatis>入门五 查询的返回值处理
select : 返回对象: <select id = " " resultType= "对象的全类名" /> List: <sele ...
- Python习题之列表排序,4种方法
def sort_list_method_1(a): return sorted(a) print(sort_list_method_1([1, 4, 2])) def sort_list_metho ...
- java诗词横版--转为竖版
import java.util.Arrays; /* 诗句横版转成竖版输出 */public class PrintPoem { public static void main(String[] a ...