小編給大家分享一下ASP.NET中Config文件是怎么讀寫的,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
方法如下:
如果是WinForm程序,需要添加引用:
System.ServiceModel
System.Configuration
App.config
NetUtilityLib
using System.Configuration; namespace pcauto { public static class ConfigHelper { //////返回*.exe.config文件中appSettings配置節(jié)的value項(xiàng) /// /// ///public static string GetAppConfig(string strKey) { string file = System.Windows.Forms.Application.ExecutablePath; Configuration config = ConfigurationManager.OpenExeConfiguration(file); foreach (string key in config.AppSettings.Settings.AllKeys) { if (key == strKey) { return config.AppSettings.Settings[strKey].Value.ToString(); } } return null; } /// ///在*.exe.config文件中appSettings配置節(jié)增加一對(duì)鍵值對(duì) /// /// /// public static void UpdateAppConfig(string newKey, string newValue) { string file = System.Windows.Forms.Application.ExecutablePath; Configuration config = ConfigurationManager.OpenExeConfiguration(file); bool exist = false; foreach (string key in config.AppSettings.Settings.AllKeys) { if (key == newKey) { exist = true; } } if (exist) { config.AppSettings.Settings.Remove(newKey); } config.AppSettings.Settings.Add(newKey, newValue); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } } }
讀示例
ConfigHelper.GetAppConfig("testkey")
寫示例
ConfigHelper.UpdateAppConfig("testkey", "abc");
以上是ASP.NET中Config文件是怎么讀寫的的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!