Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create editable configuration settings in a C# WinForms application?

I have configuration values saved in an app.config. I want to create a WinForms application which shows all the AppSettings values in a form. The user should be able to change the settings values and save them back to the app.config.

like image 976
Indigo Praveen Avatar asked Apr 21 '10 12:04

Indigo Praveen


People also ask

How do I create a new configuration in Visual Studio?

In the Active solution configuration drop-down list, choose New. The New Solution Configuration dialog box opens. In the Name text box, enter a name for the new configuration. To use the settings from an existing solution configuration, in the Copy settings from drop-down list, choose a configuration.

Which of the file is used for configuration settings?

In computing, configuration files (commonly known simply as config files) are files used to configure the parameters and initial settings for some computer programs.

What are configuration settings?

Definition(s):The set of parameters that can be changed in hardware, software, or firmware that affect the security posture and/or functionality of the information system.


3 Answers

As long as your values are in the appConfig section of the app.config file, you can simply use System.Configuration.ConfigurationManager.

ConfigurationManager.AppSettings - MSDN

Here's an old blog post explaining EXACTLY how to do what you're looking for:

Read/Write App.config

like image 185
Justin Niessner Avatar answered Oct 13 '22 09:10

Justin Niessner


If you store the settings using the Settings.settings file in the Properties folder you can just do:

Properties.Settings s = new Properties.Settings();

And then all the settings will be properties of s (you can define them as a specific type even) and if they're set as user settings you can change them. Just call Reload or Save on the instance of Settings to read/store from/to disk.

like image 38
Hans Olsson Avatar answered Oct 13 '22 09:10

Hans Olsson


I was successful with using the method Justin Niessner suggested. One caveat to watch out for: When you are testing this in visual studio, the app.config itself won't be edited if you are debugging the application. The config file that is modified is the ProjectName.vshost.exe.Config

like image 40
Adam Kalnas Avatar answered Oct 13 '22 10:10

Adam Kalnas