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. python web

    [root@xen202 wbk]# python -m SimpleHTTPServerServing HTTP on 0.0.0.0 port 8000 ...

  2. 一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网

    一个网友写的栈,问为啥不能迭代.具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网 一个网友写的栈,问为啥不能迭代.具有__iter__ 和next方法的对象叫迭代器 python视频 ...

  3. DRP之Oracle_11g数据库安装

    不知道大家在安装了Oracle数据库的时候有没有这样或那样的困惑,今天这篇文章就把Oracle数据库的安装过程理一下,作为总结,方便以后的学习. 首先,将下载的两个文件放在一个目录下解压,然后打开文件 ...

  4. JavaBean在DAO设计模式简介

    一.信息系统开发框架 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层是client,简单的来说就是浏览器. 2.显示层:JSP/Se ...

  5. Office 2016九大新功能

    Office 2016将于2015年秋季正式公布,下面是九大新功能. 预測 基于数据透视表的时间分组建模 联机分析(OLAP)下的PowerView 数据透视表(PivotTable)建模的自己主动关 ...

  6. POJ 3267-The Cow Lexicon(DP)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8252   Accepted: 3888 D ...

  7. Java程序猿面试题集(181- 199)

    Java面试题集(181-199) 摘要:这部分是包括了Java高级玩法的一些专题,对面试者和新入职的Java程序猿相信都会有帮助的. 181.  182. 183. 184. 185. 186. 1 ...

  8. 自编Ps教程—我的ps图片赞赏

    上篇讲述了主要的ps概念和操作,这里不再讲述了,主要的操作学好了,其它的都简单,下面我会把我闲暇时间天马行空的小作品上穿,以供大家闲暇时间或者工作累了的时候赞赏! 以后还会在这里上传哦!喜欢就收藏吧! ...

  9. js快速分享代码

    这是一款简单易用的文章分享工具,您只需将下面的html代码拷贝到模板中就可以实现文章快速分享功能.如果您想分享你的博客.个人网站或者企业网站等等,下面是两款不错的分享工具,值得拥有! 1. <d ...

  10. hdu3415(单调队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3415 题意:一个长度为n包含正负整数的数环,即第1个的左边是第n个.从中选一个不超过k的序列,使得序列 ...