ASP.NET中Config文件的讀寫示例
更新時間:2017年08月02日 08:58:33 作者:尋i
通常我們在.NET開發(fā)過程中,會接觸二種類型的配置文件:config文件,xml文件,下面這篇文章主要給大家介紹了關(guān)于ASP.NET中Config文件讀寫的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
本文主要給大家介紹了關(guān)于ASP.NET中Config讀寫示例的相關(guān)內(nèi)容,分享出來供大家參考學習,話不多說,來一起看看詳細的介紹吧。
方法如下:
如果是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項 ///</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)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持
相關(guān)文章
ASP.NET?Core使用EF創(chuàng)建模型(包含屬性、排除屬性、主鍵和生成值)
這篇文章介紹了ASP.NET?Core使用EF創(chuàng)建模型的的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04asp.net實現(xiàn)調(diào)用帶有輸出參數(shù)的存儲過程實例
這篇文章主要介紹了asp.net實現(xiàn)調(diào)用帶有輸出參數(shù)的存儲過程,結(jié)合實例形式分析了前端基于jQuery的ajax調(diào)用及后臺存儲過程調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-03-03