ASP.NET中Config文件是怎么讀寫的-創(chuàng)新互聯(lián)

小編給大家分享一下ASP.NET中Config文件是怎么讀寫的,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)是一家集成都網站建設、做網站、網站頁面設計、網站優(yōu)化SEO優(yōu)化為一體的專業(yè)網站設計公司,已為成都等多地近百家企業(yè)提供網站建設服務。追求良好的瀏覽體驗,以探求精品塑造與理念升華,設計最適合用戶的網站頁面。 合作只是第一步,服務才是根本,我們始終堅持講誠信,負責任的原則,為您進行細心、貼心、認真的服務,與眾多客戶在蓬勃發(fā)展的市場環(huán)境中,互促共生。

方法如下:

如果是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");

以上是ASP.NET中Config文件是怎么讀寫的的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網站欄目:ASP.NET中Config文件是怎么讀寫的-創(chuàng)新互聯(lián)
標題網址:http://bm7419.com/article0/goioo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供Google、移動網站建設、微信公眾號、企業(yè)網站制作、商城網站、網站內鏈

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網站建設