Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Config File Reading and Writing in ASP.NET

Shulou Source: shulou.com Published: 2022-06-03 13:32:41 10月18日 Update

This article mainly introduces the example analysis of reading and writing Config files in ASP.NET, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

The methods are as follows:

If it is a WinForm program, you need to add a reference:

System.ServiceModel

System.Configuration

App.config

NetUtilityLib

Using System.Configuration;namespace pcauto {public static class ConfigHelper {/ return the value entry / public static string GetAppConfig (string strKey) of the appSettings configuration section in the * .exe.config file {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.Value.ToString ();}} return null;} / add a pair of keys to the appSettings configuration section in the * .exe.config file / 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 the example

ConfigHelper.GetAppConfig ("testkey")

Write an example

ConfigHelper.UpdateAppConfig ("testkey", "abc"); the above is all the contents of the article "sample Analysis of Config File Reading and Writing in ASP.NET". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Documents examples analysis content articles configuration value interests guys partners methods more knowledge procedures industry information information channels channels references help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information vpn macOS Shulou Tech Info