如何在 c# winforms 中的 app.config 文件上动态更新应用程序设置键值对

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

如何在 c# winforms 中动态更新 app.config 文件中的应用程序设置键值。 Key,value 如下所示

<appSettings>
    <add key="logPath" value="C:\EventLogs" />
    <add key="isScreenCaptureMode" value="false" />
    <add key="isStartOnSysStartUp" value="false" />
</appSettings>

我相信这就是您要找的:

using System.Configuration;

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