日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 动态获取、修改、更新配置文件 实现思路

發布時間:2023/12/18 C# 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 动态获取、修改、更新配置文件 实现思路 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

???????

? ? ? ?1、添加System.Configuration.dll引用;程序中添加using System.Configuration;

? ? ? ?讀取數據:(tbHost為文本控件)

[csharp] view plain copy print?
  • tbHost.Text?=?ConfigurationManager.AppSettings["host"].ToString();??
  • tbHost.Text = ConfigurationManager.AppSettings["host"].ToString();

    ? ? ? ?2、修改、更新數據

    [csharp] view plain copy print?
  • private?void?btnOk_Click(object?sender,?EventArgs?e)??
  • ????????{??
  • ????????????if?(tbHost.Text.Trim().Equals("")?||?tbUserid.Text.Trim().Equals(""))?return;??
  • ??
  • ????????????//?修改并更新配置文件??
  • ????????????UpdateConfig("host",?tbHost.Text.Trim());??
  • ????????????UpdateConfig("userid",?tbUserid.Text.Trim());??
  • ????????}??
  • ??
  • ????????private?void?UpdateConfig(string?key,string?value)??
  • ????????{??
  • ????????????//?通過Xml方式(需using?System.xml;)??
  • ????????????//XmlDocument?doc?=?new?XmlDocument();??
  • ????????????//doc.Load(Application.ExecutablePath?+?".config");??
  • ????????????//XmlNode?node?=?doc.SelectSingleNode(@"//add[@key='"?+?key?+?"']");?//?定位到add節點??
  • ????????????//XmlElement?element?=?(XmlElement)node;??
  • ????????????//element.SetAttribute("value",?value);?//?賦值??
  • ????????????//doc.Save(Application.ExecutablePath?+?".config");??
  • ????????????//ConfigurationManager.RefreshSection("appSetting");?//?刷新節點??
  • ??
  • ????????????//?利用Configuration??
  • ????????????Configuration?config?=?ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);??
  • ????????????config.AppSettings.Settings[key].Value?=?value;??
  • ????????????config.Save(ConfigurationSaveMode.Full);??
  • ????????????ConfigurationManager.RefreshSection("appSettings");??
  • ????????}??
  • private void btnOk_Click(object sender, EventArgs e){if (tbHost.Text.Trim().Equals("") || tbUserid.Text.Trim().Equals("")) return;// 修改并更新配置文件UpdateConfig("host", tbHost.Text.Trim());UpdateConfig("userid", tbUserid.Text.Trim());}private void UpdateConfig(string key,string value){// 通過Xml方式(需using System.xml;)//XmlDocument doc = new XmlDocument();//doc.Load(Application.ExecutablePath + ".config");//XmlNode node = doc.SelectSingleNode(@"//add[@key='" + key + "']"); // 定位到add節點//XmlElement element = (XmlElement)node;//element.SetAttribute("value", value); // 賦值//doc.Save(Application.ExecutablePath + ".config");//ConfigurationManager.RefreshSection("appSetting"); // 刷新節點// 利用ConfigurationConfiguration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);config.AppSettings.Settings[key].Value = value;config.Save(ConfigurationSaveMode.Full);ConfigurationManager.RefreshSection("appSettings");}


    ? ? ? ?說明:經常遇到的問題是修改數據后配置文件未能立即生效,再次讀取時依然是修改前的數據。參考上述代碼時,如果還遇到這個問題,可嘗試把“Xml方式”(即UpdateConfig()中注釋部分取消注釋)和Congiguration方式一起使用。

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的C# 动态获取、修改、更新配置文件 实现思路的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。