ASP.NET中Config文件的讀寫(xiě)示例
本文主要給大家介紹了關(guān)于ASP.NET中Config讀寫(xiě)示例的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),話不多說(shuō),來(lái)一起看看詳細(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é)增加一對(duì)鍵值對(duì)
///</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")
寫(xiě)示例
ConfigHelper.UpdateAppConfig("testkey", "abc");
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持
相關(guān)文章
ASP.NET中Response.BufferOutput屬性的使用技巧
這篇文章介紹了ASP.NET中Response.BufferOutput屬性的使用技巧,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
Ajax異步無(wú)刷新對(duì)局部數(shù)據(jù)更新
Ajax異步無(wú)刷新對(duì)局部數(shù)據(jù)更新的實(shí)例2013-03-03
asp.net 光棒效應(yīng)實(shí)現(xiàn)代碼
asp.net 光棒效應(yīng)(今天剛剛學(xué)到的)2009-12-12
區(qū)分ASP.NET中g(shù)et方法和post方法
我們都知道,get是從服務(wù)器上獲取數(shù)據(jù),post是向服務(wù)器上傳數(shù)據(jù)。本文主要介紹ASP.NET中g(shù)et方法和post方法的區(qū)別,需要的朋友可以參考下2015-10-10
Asp.Net如何將多個(gè)RadioButton指定在一個(gè)組中
將多個(gè)RadioButton指定在一個(gè)組中,實(shí)現(xiàn)其實(shí)很簡(jiǎn)單,一句代碼即可,具體如下,希望對(duì)大家有所幫助2013-12-12
如何解決Ajax請(qǐng)求結(jié)果的緩存問(wèn)題說(shuō)明
2013-03-03
.NET core高性能對(duì)象轉(zhuǎn)換示例代碼
這篇文章主要給大家介紹了關(guān)于.NET CORE高性能對(duì)象轉(zhuǎn)換的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
ASP.NET Core中修改配置文件后自動(dòng)加載新配置的方法詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中修改配置文件后自動(dòng)加載新配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

