vijesh
vijesh

Reputation: 1173

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>

Upvotes: 7

Views: 12250

Answers (2)

vijesh
vijesh

Reputation: 1173

Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings["logPath"].Value = DateTime.Now.ToString("yyyy-MM-dd");
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");

Upvotes: 12

Sabri
Sabri

Reputation: 199

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");

Upvotes: 2

Related Questions