怎么在ASP.NET中读写配置文件

  介绍

今天就跟大家聊聊有关怎么在ASP。净中读写配置文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强>方法如下:

如果是WinForm程序,需要添加引用:

<李>

System.ServiceModel

<李>

System.Configuration

<强>应用程序。配置

& lt; ? xml  version=?.0“,编码=皍tf-8", ?比;   & lt; configuration>   ,& lt; appSettings>   ,& lt; add 关键=皌estkey" https://www.yisu.com/zixun/, value=" 0 ">      

<强> NetUtilityLib

using  System.Configuration;   namespace  pcauto   {   ,public  static  class  ConfigHelper   {大敌;///& lt;才能summary>,,///才能返回* .exe.config文件中appSettings配置节项的值,,///& lt;才能/summary>,,///& lt;才能param  name=皊trKey"祝辞& lt;/param>,,///& lt;才能returns> & lt;/returns>,   public 才能static  string  GetAppConfig (string  strKey)   {才能   ,,string  file =, System.Windows.Forms.Application.ExecutablePath;   ,,Configuration  config =, ConfigurationManager.OpenExeConfiguration(文件),,   ,,foreach  (string  key  config.AppSettings.Settings.AllKeys拷贝),{,   ,,,if  (key ==, strKey), {,   ,,,,return  config.AppSettings.Settings [strKey] .Value.ToString (),,   ,,,},   ,,}   ,,return 零;   ,,}///& lt;才能summary>,,///才能在* .exe.config文件中appSettings配置节增加一对键值对,,///& lt;才能/summary>,///& lt;才能param  name=皀ewKey"祝辞& lt;/param>,///& lt;才能param  name=皀ewValue"祝辞& lt;/param>,   public 才能static  void  UpdateAppConfig (string  newKey, string  newValue), {,   ,,string  file =, System.Windows.Forms.Application.ExecutablePath;   ,,Configuration  config =, ConfigurationManager.OpenExeConfiguration(文件),,   ,,bool  exist =,假的,,   ,,foreach  (string  key  config.AppSettings.Settings.AllKeys拷贝),{,   ,,,if  (key ==, newKey), {, exist =,真的,,},   ,,},   ,,if (存在),{,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中读写配置文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

怎么在ASP.NET中读写配置文件