变化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文件以及重启程序
//重启主程序 //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Locatio ...
- 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#中怎样获取默认配置文件App.config中配置的键值对内容
场景 在新建一个程序后,项目中会有一个默认配置文件App.config 一般会将一些配置文件信息,比如连接数据库的字符串等信息存在此配置文件中. 怎样在代码中获取自己配置的键值对信息. 注: 博客主页 ...
- C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案
新建C#项目,在app.config中添加了appSettings项,运行时出现"配置系统未能初始化"的错误,MSDN里写到,如果配置文件中包含 configSections 元素 ...
- 关于<appSettings file="app.config" >引用外部文件的配置值
web.config文件中,appSetting节点引用了外部的配置文件, <appSettings file="app.config"> </appSettin ...
- 读取App.config自定义标签的值
一:程序截图 二:具体代码 config配置: <?xml version="1.0" encoding="utf-8" ?> <config ...
- C# - 使用ConfigurationManager保存数据到App.config
1. ConfigurationManager的命名空间:using System.Configuration; 2. To be able to save you have to use a con ...
- c# 修改winform中app.config的配置值
public bool ChangeConfig(string AppKey,string AppValue) { bool result = true; try { XmlDocument xDoc ...
随机推荐
- C++开源代码项目汇总
Google的C++开源代码项目 v8 - V8 JavaScript EngineV8 是 Google 的开源 JavaScript 引擎.V8 采用 C++ 编写,可在谷歌浏览器(来自 Go ...
- LIS小结(O(∩_∩)O~哄哄)
~\(≧▽≦)/~啦啦啦,昨天说的是LCS,今天我们要学习的是LIS,什么是LIS呢? LIS: 最长有序子序列(递增/递减/非递增/非递减)这么说还是有些模糊,举个例子: 在一个无序的序列a1,a ...
- 成功为Android系统配上了GNU开发环境
单击此处获得本文的最新更新 经过一周的艰苦努力,成功为我的小米2手机适配上了全功能的GNU开发环境,完全兼容GNU/LINUX(Android自带的bionic.linker真心不好 ...
- 《sql---教学反馈系统-阶段项目1》
--修改列 --把 "Address" 栏位改名为 "Addr".这可以用以下的指令达成: --ALTER table customer change Addr ...
- Android照片墙完整版,的完美结合LruCache和DiskLruCache
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/34093441 在上一篇文章其中,我们学习了DiskLruCache的概念和基本使用 ...
- ACdream 1135(MST-最小生成树边上2个值,维护第一个最小的前提下让还有一个最小)
F - MST Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...
- ZooKeeper的安装、配置、启动和使用(一)——单机模式
ZooKeeper的安装.配置.启动和使用(一)——单机模式 ZooKeeper的安装非常简单,它的工作模式分为单机模式.集群模式和伪集群模式,本博客旨在总结ZooKeeper单机模式下如何安装.配置 ...
- 基于TFTP协议的远程升级设计
说明:因为CSDN博客编辑器对word格式近乎不支持.因此对表格使用了图片方式(最后一个表格未使用图片格式.大家能够看看效果),CSDN博客编辑器上传图片十分不人性化(直接复制图片是不显示的),因此本 ...
- C++编程命名规范
原地址:http://www.cnblogs.com/joinclear/archive/2013/02/21/2921422.html C++编程命名规范 0前言 根据多年工作经验和其它命名规范整理 ...
- spark的action和transformations汇集
汇总了Spark支持的Transformations 和Actions 用于备忘! 參考 http://spark.apache.org/docs/latest/programming-guide.h ...