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其中值,并保存的更多相关文章

  1. Winform读写App.config文件以及重启程序

    //重启主程序 //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Locatio ...

  2. winform程序读取和改写配置文件App.config元素的值

    winform程序读取和改写配置文件App.config元素的值 2016-05-16 17:49 by newbirth, 2412 阅读, 0 评论, 收藏, 编辑 1 2 3 4 5 6 7 & ...

  3. .net里面<app.config>中value值不能填写特殊符号问题

    配置app.config或web.config的时候,经常要填写value值, 但是value值不能包含特殊字符,需要转义, 分享一下转义字符 App.config 实际上是 xml 文件,在标准 x ...

  4. C#中怎样获取默认配置文件App.config中配置的键值对内容

    场景 在新建一个程序后,项目中会有一个默认配置文件App.config 一般会将一些配置文件信息,比如连接数据库的字符串等信息存在此配置文件中. 怎样在代码中获取自己配置的键值对信息. 注: 博客主页 ...

  5. C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案

    新建C#项目,在app.config中添加了appSettings项,运行时出现"配置系统未能初始化"的错误,MSDN里写到,如果配置文件中包含 configSections 元素 ...

  6. 关于<appSettings file="app.config" >引用外部文件的配置值

    web.config文件中,appSetting节点引用了外部的配置文件, <appSettings file="app.config"> </appSettin ...

  7. 读取App.config自定义标签的值

    一:程序截图 二:具体代码 config配置: <?xml version="1.0" encoding="utf-8" ?> <config ...

  8. C# - 使用ConfigurationManager保存数据到App.config

    1. ConfigurationManager的命名空间:using System.Configuration; 2. To be able to save you have to use a con ...

  9. c# 修改winform中app.config的配置值

    public bool ChangeConfig(string AppKey,string AppValue) { bool result = true; try { XmlDocument xDoc ...

随机推荐

  1. GrabCut--Opencv篇

    最近因为工作需要,需要实现一个Grabcut函数.Opencv已经提供此函数,今天把opencv的例程拿出来跑了一下,对于简单的背景实现效果还不错. OpenCV中的GrabCut算法是依据<& ...

  2. 第二章排错的工具:调试器Windbg(下)

    感谢博主 http://book.51cto.com/art/200711/59874.htm 2.2  读懂机器的语言:汇编,CPU执行指令的最小单元2.2.1  需要用汇编来排错的常见情况 汇编是 ...

  3. POJ 3974 最长回文字串(manacher算法)

    题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...

  4. 新发现IM项目Rabbitim(使用msys或者cygwin编译安装),FileZilla(wxWidget开发)

    https://github.com/KangLin/rabbitim/blob/master/docs/INSTALL.md https://github.com/KangLin/rabbitim ...

  5. elf格式分析

    近期研究了一下elf文件格式,发现好多资料写的都比較繁琐,可能会严重打击学习者的热情,我把自己研究的结果和大家分享,希望我的描写叙述可以简洁一些. 一.基础知识 elf是一种文件格式,用于存储Linu ...

  6. Echart饼图、柱状图、折线图(pie、bar、line)加入点击事件

    var myChart= echarts.init(document.getElementById('myChart')); myChart.on('click', function (param) ...

  7. Hadoop大数据面试--Hadoop篇

    本篇大部分内容參考网上,当中性能部分參考:http://blog.cloudera.com/blog/2009/12/7-tips-for-improving-mapreduce-performanc ...

  8. httpclient超时总结(转)

    Httpclient超时 背景: 网站这边多次因为httpclient调用超时时间没设置好导致关掉,影响非常不好,而且问题重复出现,查看网络,没有比较明确介绍httpclient所有超时相关的设置(大 ...

  9. cookie是指web浏览器存储的少量数据,该数据会在每次请求一个相关的URL时自动传到服务器中(转)

    基本概念:cookie是指web浏览器存储的少量数据,该数据会在每次请求一个相关的URL时自动传到服务器中. 以博客园为例,我们看看cookie有哪些属性: 1.Name:cookie的名称: 2.V ...

  10. RAC优化大框架的分配(jumbo frame)

    RAC优化大框架的分配(jumbo frame) 首先讲讲MTU的概念:在网络通信中,有个MTU(Max Transmission Unit)的概念,即网络传输中最大帧的大小,这个值默认是1500By ...