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. 在HTML中如何隐藏某段文字具体该怎么实现

    <p style="display:none;"> 需要隐藏的文字....... </p> <div style="display:none ...

  2. syslog-ng 3.4.3 发布,强烈建议升级 - 开源中国社区

    syslog-ng 3.4.3 发布,强烈建议升级 - 开源中国社区 syslog-ng 3.4.3 发布,强烈建议升级    oschina 发布于: 2013年08月16日 (2评)    分享到 ...

  3. acdream 1222 Quantization Problem [dp]

    称号:acdream 1222 Quantization Problem 题意:给出一个序列 a ,然后给出一个 n * m 的矩阵,让你从这个矩阵中选出一个序列k,使得sum(abs(ki - ai ...

  4. [置顶] CentOS release 5.4 (Final)重置root密码(图文)

  5. C strstr() 函数

    包含文件:string.h 函数名: strstr 函数原型:extern char *strstr(const char *str1, const char *str2); 语法:* strstr( ...

  6. zoj2760(最大流)

    传送门:How Many Shortest Path 题意:给出n个点,和n*n的矩阵表示有向图.a[i][j]为-1表示i到j没有路径:不为-1则表示i到j的路径长度.给出一个vs和vt,要求vs到 ...

  7. spring集成 JedisCluster 连接 redis3.0 集群

    最近在公司做了 jedisCluster整合spring 的配置, 分享如下 客户端采用最新的jedis 2.7 1. maven依赖: <dependency> <groupId& ...

  8. 智能指针 shared_ptr 解析

    近期正在进行<Effective C++>的第二遍阅读,书里面多个条款涉及到了shared_ptr智能指针,介绍的太分散,学习起来麻烦.写篇blog整理一下. LinJM   @HQU s ...

  9. SE 2014年4月29日

    交换网络中有vlan1 到20个vlan,要求使用MSTP技术实现vlan的负载分担. SW2为实例1(vlan1-vlan10)的主根,SW3为备根 SW3为实例2(vlan11-vlan20)的主 ...

  10. SE 2014年4月2日

    一 描述OSPF协议 LSA(Type 1~5)的名称,始发者以及特点 第一类LSA (router lsa)该类lSA为启动了ospf进程的所有路由器都可以产生,该类LSA主要含有本地路由器的接口状 ...