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. openCV之头文件分析

    我们利用openCV开源库进行项目开发时,往往要牵涉到头文件的添加问题,而openCV中头文件众多,该如何选择呢?下面对openCV2.4.10的头文件进行一个简单的梳理,以便能够快速的添加对应的头文 ...

  2. WebSocket是一种协议

    WebSocket,并非HTML 5独有,WebSocket是一种协议.只是在handshake的时候,发送的链接信息头和HTTP相似.HTML 5只是实现了WebSocket的客户端.其实,难点在于 ...

  3. haproxy 看到的是https,后台是http的原因

    https://www.zjtest6.com/admin/api/menu haproxy 日志: Jun 24 13:23:02 localhost haproxy[23205]: 192.168 ...

  4. html name id, 与服务器交互必须有name

    html name id, 与服务器交互必须有name 在HTML中元素的ID和Name的区别和联系. 今天写了个测试,在php脚本里怎么也获取不到$_POST['userName']的值,经检查在h ...

  5. CentOS 6.4下编译安装MySQL 5.6.16

    一.卸载旧版本号MySql 1.rpm卸载: 1> 检查安装包: rpm -qa | grep mysql 2> 普通删除: rpm -e mysql-5.6.16.rpm 3> 强 ...

  6. How to write simple HTTP proxy with Boost.Asio

    How to write simple HTTP proxy with Boost.Asio How to write simple HTTP proxy with Boost.Asio Russia ...

  7. Scriptcase价格调整(五折销售)

    芬兰诺基亚!芬兰诺基亚!最大手机公司倒闭了!王八蛋老板埃洛普,吃喝嫖赌欠下了3.5个亿,带着他的小姨子跑了!我们没有办法,拿着手机抵工资!原价都是一千多.两千多.三千多的手机,统统二百块!统统二百块! ...

  8. hdu 1240 Asteroids!(BFS)

    题目链接:点击链接 简单BFS,和二维的做法相同(需注意坐标) 题目大意:三维的空间里,给出起点和终点,“O”表示能走,“X”表示不能走,计算最少的步数 #include <iostream&g ...

  9. windows 7多点触摸开发

    win7 触摸屏系统应用广泛,软件操作方便,功能强大,现以被很多硬件厂商应用. 我曾用一台装有win7 的汉王平板电脑进行了多点触摸软件的开发.      开发环境及条件: 1. 平板电脑+ win7 ...

  10. 断言Assert的使用

     转载地址:http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数,事实上,它居然是个宏 ...