Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update app settings key value pair dynamically on app.config file in c# winforms

How to update app settings key, value dynamically on app.config file in c# winforms. Key,value are listed below

<appSettings>
    <add key="logPath" value="C:\EventLogs" />
    <add key="isScreenCaptureMode" value="false" />
    <add key="isStartOnSysStartUp" value="false" />
</appSettings>
like image 254
vijesh Avatar asked Mar 27 '18 09:03

vijesh


People also ask

Can we dynamically change the key value in web config?

The sample source code allows you to change the key and value pair. There is no need to open web. config file. The application also allows you to update and delete the key/value pair.

Can we have multiple App config?

The App. config that holds all the required configuration for a GWM based solution to work is stored inside the SAP Service Reference folder under the project file. For running a GWM based solution you need to either have only one App.

What information is contained in the App config file?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


2 Answers

Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings["logPath"].Value = DateTime.Now.ToString("yyyy-MM-dd");
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
like image 95
vijesh Avatar answered Sep 29 '22 16:09

vijesh


I believe this is what you are looking for:

using System.Configuration;

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings["isScreenCaptureMode"].Value = "true";
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
like image 26
Sabri Avatar answered Sep 29 '22 16:09

Sabri