Usually we're in. NET development process, will be exposed to two types of configuration files: Config file, xml file, The following article mainly introduces to you about the config file in ASP. Read and write information, the text through the sample code introduced in very detailed, the need for friends can refer to, let's look at the following together.
This article mainly introduces to you about the config read and write example of ASP, share it for everyone to refer to the study, words do not say, come together to see the detailed introduction bar.
Here's how:
If this is a WinForm program, you need to add a reference:
System.ServiceModel
System.Configuration
App.
<?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.config text The value item in the appsettings configuration section///</summary>///<param name= "strkey" ></param>///<returns></r eturns> public static string Getappconfig (String strkey) {String file = System.Windows.Forms.Application.Executab Lepath; 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.config file appsettings configuration section adds a pair of key-value pairs///</summary>///<param name= "NewKey" >& lt;/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 an example
Confighelper.updateappconfig ("TestKey", "abc");
Summarize