国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

【C#】#103 動態修改App.config配置文件

2019-11-17 02:34:30
字體:
來源:轉載
供稿:網友

【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("http://appSettings");                xElem1 = (XmlElement)xNode.SelectSingleNode("http://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("http://appSettings");                xElem1 = (XmlElement)xNode.SelectSingleNode("http://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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 剑阁县| 宝鸡市| 岳阳市| 常宁市| 桃源县| 三河市| 新闻| 武隆县| 岳西县| 白玉县| 会宁县| 苏州市| 襄城县| 景德镇市| 秀山| 柳河县| 黄浦区| 苍溪县| 北京市| 宁南县| 昌图县| 合川市| 炉霍县| 邵东县| 峨眉山市| 黔东| 广西| 上思县| 湟中县| 宁城县| 盐城市| 宝应县| 沁水县| 吕梁市| 垫江县| 丰宁| 厦门市| 新疆| 同江市| 昌吉市| 榆树市|