国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET中Config文件的讀寫示例

2024-07-10 13:32:36
字體:
來源:轉載
供稿:網友

本文主要給大家介紹了關于ASP.NET中Config讀寫示例的相關內容,分享出來供大家參考學習,話不多說,來一起看看詳細的介紹吧。

方法如下:

如果是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配置節的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配置節增加一對鍵值對    ///</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");

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新田县| 信宜市| 富民县| 凭祥市| 孟村| 隆尧县| 翁牛特旗| 安阳市| 博乐市| 大洼县| 兴山县| 来凤县| 沅陵县| 东城区| 鄄城县| 佛教| 石楼县| 龙江县| 阳东县| 忻城县| 宝山区| 青龙| 福建省| 潼南县| 岳西县| 日照市| 霞浦县| 苍山县| 武穴市| 盐山县| 德安县| 南宫市| 东乡| 望城县| 镶黄旗| 凤冈县| 交口县| 崇明县| 陵川县| 芜湖市| 浮梁县|