介绍
今天就跟大家聊聊有关怎么在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中读写配置文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。