zoukankan      html  css  js  c++  java
  • c# 修改winform中app.config的配置值

         public bool ChangeConfig(string AppKey,string AppValue)
            {
                bool result = true;
                try
                {
                    XmlDocument xDoc = new XmlDocument();
                    //获取App.config文件绝对路径
                    String basePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    basePath = basePath.Substring(0, basePath.Length - 10);
                    String path = basePath + "App.config";
                    xDoc.Load(path);
    
                    XmlNode xNode;
                    XmlElement xElem1;
                    XmlElement xElem2;
                    //修改完文件内容,还需要修改缓存里面的配置内容,使得刚修改完即可用
                    //如果不修改缓存,需要等到关闭程序,在启动,才可使用修改后的配置信息
                    Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    xNode = xDoc.SelectSingleNode("//appSettings");
                    xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                    if (xElem1 != null)
                    {
                        xElem1.SetAttribute("value", AppValue);
                        cfa.AppSettings.Settings[AppKey].Value = AppValue;
                    }
                    else
                    {
                        xElem2 = xDoc.CreateElement("add");
                        xElem2.SetAttribute("key", AppKey);
                        xElem2.SetAttribute("value", AppValue);
                        xNode.AppendChild(xElem2);
                        cfa.AppSettings.Settings.Add(AppKey, AppValue);
                    }
                    //改变缓存中的配置文件信息(读取出来才会是最新的配置)
                    cfa.Save();
                    ConfigurationManager.RefreshSection("appSettings");
                    xDoc.Save(path);
                }
                catch (Exception ex)
                {
                    _log.Error("修改App.config文件出错:"+ex.Message);
                    result = false;
                }
                return result;
            }
  • 相关阅读:
    Vue3+Element-Plus-admin
    uniapp UI库推荐 uView官网/文档
    js 获取本月月初和月末时间操作
    el-dialog 无法弹出来
    Vue核心技术 Vue+Vue-Router+Vuex+SSR实战精讲
    sed 相关命令
    docker 启动 ubuntu
    vue 配置 history 模式
    typescript 相关
    fiddler 图片下载
  • 原文地址:https://www.cnblogs.com/25miao/p/7206984.html
Copyright © 2011-2022 走看看