c# - ConfigurationManager doesn't save settings - Stack Overflow
static void AddUpdateAppSettings(string key, string value) { try { var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; if (settings[key] == null) { settings.Add(key, value); } else { settings[key].Value = value; } configFile.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); } catch (ConfigurationErrorsException) { Console.WriteLine("Error writing app settings"); } } static string ReadSetting(string key) { try { var appSettings = ConfigurationManager.AppSettings; return appSettings[key] ?? "Not Found"; } catch (ConfigurationErrorsException) { return "Error reading app settings"; } }
注意:
如果正在调试(并且尚未禁用 vshost 进程),则当应用程序停止时,executable.vshost.exe.config 将再次替换为 yourexecutable.exe.config。
因此,可能看不到所做的任何更改!(如果在调试时停在断点处,并在进行修改和调用刷新部分后查看文件,则会看到所做的更改)。
这没什么可担心的,当应用程序直接从bin部署或启动时,这一切都有效...
避免手写XML