问题
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>
回答1:
Configuration configuration = ConfigurationManager.
OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
configuration.AppSettings.Settings["logPath"].Value = DateTime.Now.ToString("yyyy-MM-dd");
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
回答2:
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");
来源:https://stackoverflow.com/questions/49509701/how-to-update-app-settings-key-value-pair-dynamically-on-app-config-file-in-c-sh