Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationManager.AppSettings - How to modify and save?

It might sound too trival to ask and I do the same thing as suggested in articles, yet it doesn't work as expected. Hope someone can point me to the right direction.

I would like to save the usersettings per AppSettings.

Once the Winform is closed I trigger this:

conf.Configuration config =             ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  if (ConfigurationManager.AppSettings["IntegrateWithPerforce"] != null)     ConfigurationManager.AppSettings["IntegrateWithPerforce"] =                                             e.Payload.IntegrateCheckBox.ToString(); else     config.AppSettings.Settings.Add("IntegrateWithPerforce",                                            e.Payload.IntegrateCheckBox.ToString());  config.Save(ConfigurationSaveMode.Modified); 

So the first time when the entry doesnt exist yet, it would simply create it, otherwise it would modify the existing entry. However this doesn't save.

1) What am I doing wrong?

2) Where am I expecting the usersettings for App settings to be saved again? Is it in the Debug folder or in C:\Documents and Settings\USERNAME\Local Settings\Application Data folder?

like image 612
Houman Avatar asked Mar 11 '11 15:03

Houman


1 Answers

I know I'm late :) But this how i do it:

public static void AddOrUpdateAppSettings(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");     } } 

For more information look at MSDN

like image 69
Wahid Bitar Avatar answered Sep 23 '22 22:09

Wahid Bitar