Web. config read/write and Configuration

Source: Internet
Author: User
If flexible operations and read/write configuration files are required in. net1.1, a configuration file management class is encapsulated in the project for read/write operations. In. net2.0, The configurationmanager and webconfigurationmanager classes can be used to manage configuration files. The configurationmanager class is in system. configuration, and the webconfigurationmanager class is in system. Web. configuration. According to msdn Program We recommend that you use the system. Web. configuration. webconfigurationmanager class instead of the system. configuration. configurationmanager class.

The following is a simple example to illustrate how to use webconfigurationmanager to operate the configuration file:

/Open the configuration file
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
/Obtain the deleteworker Node
Appsettingssection appsection = (appsettingssection) config. getsection ("appsettings ");
/Add an element to the deleteworker node.
Appsection. settings. Add ("addkey1", "key1's value ");
Appsection. settings. Add ("addkey2", "key2's value ");
Config. Save ();

RunCodeThen you can see the changes in the configuration file:

<Deleetask>
<Add key = "addkey1" value = "key1's value"/>
<Add key = "addkey2" value = "key2's value"/>
</Appsettings>

It is also convenient to modify and delete nodes or attributes:

/Open the configuration file
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
/Obtain the deleteworker Node
Appsettingssection appsection = (appsettingssection) config. getsection ("appsettings ");
/Delete the elements in the deleteworker Node
Appsection. settings. Remove ("addkey1 ");
/Modify the elements in the deleteworker Node
Appsection. settings ["addkey2"]. value = "Modify key2's value ";
Config. Save ();

Configuration file:

<Deleetask>
<Add key = "addkey2" value = "Modify key2's value"/>
</Appsettings>

This is a simple method.
Configurationmanager. getsection ("path ")

In addition, set the size of the uploaded file in httprutime (as if this is the case.

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.