zoukankan      html  css  js  c++  java
  • 更改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");
            }
        }
    }
  • 相关阅读:
    C# TransactionScope 使用
    .Net 4.5 的async 和await 的简单理解使用
    图片的等比缩放
    IIS 8 下使用 WCF
    SQL Server 中字符串中包含字符串变量的表示方法
    jsTree 的简单用法--异步加载和刷新数据
    webService 部署以后参数输入框不能显示
    js 节点属性
    js 数组排序
    js 时间格式化 -- 时间加减实现
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7224518.html
Copyright © 2011-2022 走看看