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"); } } } } 
  • 2
    Why so hard? Why not just through Settings ? I think you are creating problems for yourself. - VladD
  • It is possible and create, solely because of the inexperienced. It can be more specific: what kind of Settings where you can store whether you can edit not from the application. - Varfalamey Isoldin

1 answer 1

First, add the fields to the project settings enter image description here

Then in the code change them and call save (do not forget to connect the namespace):

 using ConsoleApp1.Properties; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Settings.Default.Id = 10; Settings.Default.Status = "Статус"; Settings.Default.Save(); } } } 

And now you can make sure that the settings are saved for a specific user:

enter image description here

  • And how to tell these settings where to store the settings file? - Varfalamey Isoldin
  • 2
    @ VarfalameyIzoldin: This is usually not necessary, the file is stored in %APPDATA%\Имя вашего приложения\... - VladD 5:26 pm
  • 2
    @ Varfalamey Isoldin, I’m adding a little more to VladD's answer. To do this, you need to create your own provider for the settings. Description here stackoverflow.com/questions/2265271/… , but this is usually meaningless. - Sergey Ignakhin
  • And in what cases is it then necessary to use ConfigurationManager and AppSettings? - Varfalamey Isoldin
  • one
    @ Varfalamey Isoldin, for example, in the case when you need to generate field names for example in dynamics. - Sergey Ignakhin