【C#】#103 动态修改App.config配置文件

C/S模式 下的 App.config 配置文件的AppSetting节点,支持配置信息现改现用,并可以持久保存

一. 先了解一下如何获取 配置信息里面的内容【获取配置信息推荐使用这个】

1.1 获取方法一:获取之前需要引用命名空间: using System.Configuration;

ConfigurationManager.AppSettings["key"]

1.2获取方法二:使用XML类,直接 Load 配置文件,然后读取 AppSetting节点下的信息【不推荐使用】

二、写入,或者修改配置I信息【推荐两个方法一起使用

2.1 方法一:使用系统自带的ConfigurationManager

存在问题:写入之后,不能持久化的保存起来,只能在程序运行的区间有作用。程序关闭,存储的信息就消失。

Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings["SMTP"].Value = value;

2.2 方法二: 使用XML类加载配置文件,在添加XMLNode节点,或者修改【直接修改原文件】

存在问题:改修改/添加结束,不能马上使用到最新的配置信息。

            try
            {
                XmlDocument xDoc = new XmlDocument();
                //获取App.config文件绝对路径
                String basePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                String path = basePath.Substring(0, basePath.Length - 10) + "App.config";

                xDoc.Load(path);
                XmlNode xNode;
                XmlElement xElem1;
                XmlElement xElem2;

                xNode = xDoc.SelectSingleNode("//appSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                if (xElem1 != null)
                    xElem1.SetAttribute("value", AppValue);
                else
                {
                    xElem2 = xDoc.CreateElement("add");
                    xElem2.SetAttribute("key", AppKey);
                    xElem2.SetAttribute("value", AppValue);
                    xNode.AppendChild(xElem2);
                }
                xDoc.Save(path);
                //Properties.Settings.Default.Reload();
            }
            catch (Exception e)
            {
                string error = e.Message;

            }

推荐的使用的方法:

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);
               
                //Properties.Settings.Default.Reload();
            }
            catch (Exception e)
            {
                string error = e.Message;

            }

可以直接下载使用

源码类:AppSettingHelper.cs