WinForm write the App. Config Profile--it Wheel series (eight)

Source: Internet
Author: User

Objective

In WinForm projects, you often need to read the app. config file. Such as:

1 var version = system.configuration.configurationmanager.appsettings["version "];

and "write", before thought of course is this:

1 ConfigurationManager.AppSettings.Set ("version","1.0.0 ");

But this writing did not succeed, do not understand why. At that time, I thought that this app. config is not allowed to write operations. For configuration information modification requirements, it can only be implemented by reading and writing XML files. I don't know, have you ever met.

Today on the internet accidentally found a way to write the app. Config, the code is as follows:

1         Private voidSetappsettingsvalue (stringKeystringvalue)2         {3             stringFile =System.Windows.Forms.Application.ExecutablePath;4Configuration config =configurationmanager.openexeconfiguration (file); 5             //determine if a node is included6             if(config. AppSettings.Settings.AllKeys.Contains (key))7             {8Config. Appsettings.settings[key]. Value =value;9             }Ten             Else One             { A                 //Adding nodes - CONFIG. APPSETTINGS.SETTINGS.ADD (key, value); -             } the CONFIG. Save (configurationsavemode.modified); -Configurationmanager.refreshsection ("appSettings"); -}
The effect is as follows:

OK, and get a wheel, hope can help everyone. Good night....

WinForm write the App. Config Profile--it Wheel series (eight)

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.