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

扶醉桌前 提交于 2019-12-23 10:24:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!