国产探花免费观看_亚洲丰满少妇自慰呻吟_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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 互助| 宜章县| 永德县| 绥德县| 无锡市| 东莞市| 宣城市| 明星| 新乐市| 含山县| 文水县| 武平县| 红桥区| 大同市| 察隅县| 日喀则市| 行唐县| 黄梅县| 茌平县| 曲沃县| 同心县| 鹤峰县| 和平区| 璧山县| 永城市| 芮城县| 常山县| 雷州市| 拉孜县| 将乐县| 宜宾市| 佛坪县| 吴堡县| 澄迈县| 汉中市| 钟祥市| 乳源| 温州市| 旬阳县| 苗栗市| 邹平县|