Good evening. Please help me understand why the following code does not save the changed settings in test.config in AppData. If the code is incorrect, please correct the test.config file is created in AppData, but the code below does not change it!
using System; using System.Configuration; using System.IO; namespace Test { class Program { static public string pathConfig = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\test"; static void Main(string[] args) { IntializeConfigurationFile(); AddUpdateAppSettings("id", "777"); Console.WriteLine(ReadSetting("id")); // вернул 555 Console.ReadKey(); } static public string ReadSetting(string key) { try { var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; if (settings[key].Value == null) { return "not found"; } else { return settings[key].Value; } } catch (ConfigurationErrorsException e) { Console.WriteLine(e.Message); return "error"; } } static void AddUpdateAppSettings(string key, string value) { try { var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = config.AppSettings.Settings; if (settings[key] == null) { settings.Add(key, value); } else { settings[key].Value = value; } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } catch (ConfigurationErrorsException e) { Console.WriteLine("Error writing app settings : {0}", e); } } static public void IntializeConfigurationFile() { if (!File.Exists(pathConfig + "\\test.config")) { string[] buffer = {"<appSettings>", "<add key='status' value='on'/>", "<add key='id' value='555'/>", "</appSettings>"}; File.WriteAllLines(pathConfig + "\\test.config", buffer); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); AppSettingsSection appSettings = config.AppSettings; appSettings.File = pathConfig + "\\test.config"; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } } } } 

Settings? I think you are creating problems for yourself. - VladD