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

  介绍

小编给大家分享一下ASP。网络中配置文件是怎么读写的,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!

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

<李>

System.ServiceModel

<李>

System.Configuration

<强>应用程序。配置

& lt; ?xml version=?.0”;编码=皍tf-8"比;   & lt; configuration>   & lt; appSettings>   & lt;添加关键=皌estkey"值=https://www.yisu.com/zixun/" 0 ">      

<强> NetUtilityLib

;
  名称空间pcauto
  {
  公共静态类ConfigHelper
  {///& lt; summary>///返回* .exe.config文件中appSettings配置节项的值///& lt;/summary>///& lt;参数name=皊trKey"祝辞& lt;/param>///& lt; returns> & lt;/returns>
  strKey GetAppConfig公共静态字符串(字符串)
  {
  字符串文件=System.Windows.Forms.Application.ExecutablePath;
  配置配置=ConfigurationManager.OpenExeConfiguration(文件);
  foreach(字符串键config.AppSettings.Settings.AllKeys) {
  如果(键==strKey) {
  返回config.AppSettings.Settings [strKey] .Value.ToString ();
  }
  }
  返回null;
  }///& lt; summary>///在* .exe.config文件中appSettings配置节增加一对键值对///& lt;/summary>///& lt;参数name=皀ewKey"祝辞& lt;/param>///& lt;参数name=皀ewValue"祝辞& lt;/param>
  公共静态孔隙UpdateAppConfig (newKey的字符串,字符串newValue) {
  字符串文件=System.Windows.Forms.Application.ExecutablePath;
  配置配置=ConfigurationManager.OpenExeConfiguration(文件);
  bool存在=false;
  foreach(字符串键config.AppSettings.Settings.AllKeys) {
  如果存在(键==newKey) {=true;}
  }
  如果(存在){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中配置文件是怎么读写的