Use a program to modify the configuration file.

Source: Internet
Author: User

Use a program to modify the configuration file.

Previously, due to the need for various configuration modifications for online verification, it was too troublesome for O & M colleagues. Today, I used a program to modify the configuration file. I noted it once and for all:

[HttpPost] public ActionResult SaveSetting (string key, string value) {if (! String. IsNullOrWhiteSpace (key )&&! String. isNullOrWhiteSpace (value) & amp; ConfigurationManager. appSettings. allKeys. contains (key) {// ConfigurationManager. appSettings. set (key, value); // var config = ConfigurationManager. openExeConfiguration (ConfigurationUserLevel. none); // The method for modifying applications such as winform var config = WebConfigurationManager. openWebConfiguration ("~ "); // Modify config. AppSettings. Settings [key]. Value = value; config. Save ();} return RedirectToAction (" Index ");}

The above is the modification method in Web applications. If it is winform or wpf,

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

.

Use"

ConfigurationManager.AppSettings.Set(key, value);  

The modification will only be changed in the cache, and will not be applied to the configuration file. After the application is restarted, the modification will become invalid.

 

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.