Example of reading and writing the Config file in ASP. NET, asp. netconfig

Source: Internet
Author: User
Tags allkeys

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.

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.