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 think the problem is that in the debug visual studio don't use the normal exeName.
it use indtead "NameApplication".host.exe
so the name of the config file is "NameApplication".host.exe.config and not "NameApplication".exe.config
and after the application close - it return to the back app.config
so if you check the wrong file or you check on the wrong time you will see that nothing changed.
Prefer <appSettings>
to <customUserSetting>
section. It is much easier to read AND write with (Web)ConfigurationManager. ConfigurationSection, ConfigurationElement and ConfigurationElementCollection require you to derive custom classes and implement custom ConfigurationProperty properties. Way too much for mere everyday mortals IMO.
Here is an example of reading and writing to web.config:
using System.Web.Configuration;
using System.Configuration;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
string oldValue = config.AppSettings.Settings["SomeKey"].Value;
config.AppSettings.Settings["SomeKey"].Value = "NewValue";
config.Save(ConfigurationSaveMode.Modified);
Before:
<appSettings>
<add key="SomeKey" value="oldValue" />
</appSettings>
After:
<appSettings>
<add key="SomeKey" value="newValue" />
</appSettings>
On how to change values in appSettings section in your app.config file:
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
does the job.
Of course better practice is Settings class but it depends on what are you after.
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
as the base question is about win forms here is the solution : ( I just changed the code by user1032413 to rflect windowsForms settings ) if it's a new key :
Configuration config = configurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Add("Key","Value");
config.Save(ConfigurationSaveMode.Modified);
if the key already exists :
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["Key"].Value="Value";
config.Save(ConfigurationSaveMode.Modified);
Perhaps you should look at adding a Settings File. (e.g. App.Settings) Creating this file will allow you to do the following:
string mysetting = App.Default.MySetting;
App.Default.MySetting = "my new setting";
This means you can edit and then change items, where the items are strongly typed, and best of all... you don't have to touch any xml before you deploy!
The result is a Application or User contextual setting.
Have a look in the "add new item" menu for the setting file.