C # Reading and modifying configuration files (Code tutorial ),

Source: Internet
Author: User

C # Reading and modifying configuration files (Code tutorial ),
Preface

Configuration files are used in many cases. configuration files are divided into two types: application configuration files and web configuration files, the maximum two configuration files are those that are updated in real time after the web configuration file is updated. The configuration files of the application are not updated in real time, and must be refreshed after the update. (ConfigurationManager needs to be updated, but Configuration does not)

Method

Before calling ConfigurationManager, you must add system. configuration. dll Assembly reference in the project.

Update the configuration file:

Configuration cft = ConfigurationManager. OpenExeconfiguration (ConfigurationUserLevel. None );

// Add

Cft. AppSettings. Settings. Add ("Key", "Name ");

// Modify

Cft .. AppSettings. Settings ["DB"]. Value = "Name ";

// Final call

Cft. Save ();

// The current configuration file is updated successfully.

ConfigurationManager. RefreshSection ("receivettings"); // The application must refresh the named node.

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.