Read-write explanation of config file in ASP.

Source: Internet
Author: User
Tags allkeys
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");
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.