关于Configuration.ConfigurationManager
生活随笔
收集整理的這篇文章主要介紹了
关于Configuration.ConfigurationManager
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Configuration.ConfigurationManager是.Net2.0的新類。在winform必須添加對(duì)System.Configuration.dll的引用才可以調(diào)用。
在做winform配置文件的時(shí)候總是不能實(shí)時(shí)讀取配置數(shù)據(jù)。因?yàn)閣eb程序中的習(xí)慣,在web程序中配置文件更改后,應(yīng)用程序會(huì)自動(dòng)重啟一次,于是配置會(huì)自動(dòng)生效。但winform程序沒有這個(gè)機(jī)制,于是Configuration.ConfigurationManager調(diào)用配置不會(huì)自動(dòng)更新。
手工實(shí)現(xiàn):拋棄Configuration.ConfigurationManager,直接讀xml文檔。
public?string?ReadAppSetting(string?key)
????????{
????????????string?xPath?=?"/configuration/appSettings//add[@key='"+key+"']";
????????????XmlDocument?doc?=?new?XmlDocument();
????????????string?exeFileName?=?System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
????????????doc.Load(exeFileName?+?".exe.config");
????????????XmlNode?node?=?doc.SelectSingleNode(xPath);
????????????return?node.Attributes["value"].Value.ToString();
????????}
在做winform配置文件的時(shí)候總是不能實(shí)時(shí)讀取配置數(shù)據(jù)。因?yàn)閣eb程序中的習(xí)慣,在web程序中配置文件更改后,應(yīng)用程序會(huì)自動(dòng)重啟一次,于是配置會(huì)自動(dòng)生效。但winform程序沒有這個(gè)機(jī)制,于是Configuration.ConfigurationManager調(diào)用配置不會(huì)自動(dòng)更新。
手工實(shí)現(xiàn):拋棄Configuration.ConfigurationManager,直接讀xml文檔。
public?string?ReadAppSetting(string?key)
????????{
????????????string?xPath?=?"/configuration/appSettings//add[@key='"+key+"']";
????????????XmlDocument?doc?=?new?XmlDocument();
????????????string?exeFileName?=?System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
????????????doc.Load(exeFileName?+?".exe.config");
????????????XmlNode?node?=?doc.SelectSingleNode(xPath);
????????????return?node.Attributes["value"].Value.ToString();
????????}
轉(zhuǎn)載于:https://www.cnblogs.com/tuyile006/archive/2008/06/22/1227667.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的关于Configuration.ConfigurationManager的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 嫁给普京的中国女人是谁
- 下一篇: 5分钟安全顾问 - 针对家庭办公室用户的