Example of reading and writing the Config file in ASP. NET, asp. netconfig
This article mainly introduces the configuration Read and Write examples in ASP. NET, and shares them for your reference. If you don't talk about them much, let's take a look at the details.
The method is as follows:
If it is a WinForm program, add reference:
- 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> // return *. exe. the value item in the configuration section of the deleettings configuration section in the config file /// </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> /// in *. exe. add a key-value pair to the deleteconfig section in the config file /// </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 ");}}}
Read example
ConfigHelper.GetAppConfig("testkey")
Write example
ConfigHelper.UpdateAppConfig("testkey", "abc");
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message to us. Thank you for your support.