ASP.NET中Config文件的讀寫示例
本文主要給大家介紹了關(guān)于ASP.NET中Config讀寫示例的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),話不多說,來一起看看詳細(xì)的介紹吧。
方法如下:
如果是WinForm程序,需要添加引用:
- System.ServiceModel
- System.Configuration
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="testkey" value="0"></add> </appSettings> </configuration>
NetUtilityLib
using System.Configuration; namespace pcauto { public static class ConfigHelper { ///<summary> ///返回*.exe.config文件中appSettings配置節(jié)的value項(xiàng) ///</summary> ///<param name="strKey"></param> ///<returns></returns> 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; } ///<summary> ///在*.exe.config文件中appSettings配置節(jié)增加一對鍵值對 ///</summary> ///<param name="newKey"></param> ///<param name="newValue"></param> 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");
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持
相關(guān)文章
ASP.NET?Core使用EF創(chuàng)建模型(包含屬性、排除屬性、主鍵和生成值)
這篇文章介紹了ASP.NET?Core使用EF創(chuàng)建模型的的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04asp.net用三層實(shí)現(xiàn)多條件檢索示例
三層將項(xiàng)目分為界面層,業(yè)務(wù)邏輯層和數(shù)據(jù)訪問層,下面為大家介紹下asp.net如何用三層實(shí)現(xiàn)多條件檢索,感興趣的朋友可以參考下2014-07-07asp.net實(shí)現(xiàn)調(diào)用帶有輸出參數(shù)的存儲過程實(shí)例
這篇文章主要介紹了asp.net實(shí)現(xiàn)調(diào)用帶有輸出參數(shù)的存儲過程,結(jié)合實(shí)例形式分析了前端基于jQuery的ajax調(diào)用及后臺存儲過程調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-03-03.net MVC使用Session驗(yàn)證用戶登錄(4)
這篇文章主要為大家詳細(xì)介紹了.net MVC使用Session驗(yàn)證用戶登錄的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04ASP.NET?MVC實(shí)現(xiàn)本地化和全球化
這篇文章介紹了ASP.NET?MVC實(shí)現(xiàn)本地化和全球化的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10