2013/07/15

Update app.config settings at runtime

using System.Configuration;

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

appSettings["WorkingMinutes"].Value = numericUpDownWorkingPeriod.Value.ToString();
appSettings["RestMinutes"].Value = numericUpDownRestPeriod.Value.ToString();
appSettings["PhotoPath"].Value = textBoxPhotoPath.Text.Trim();

config.Save();
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);


Same as above, different style.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

appSettings["key1"].Value = "value1";
appSettings["key2"].Value = "value2";

config.Save();
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

No comments:

Post a Comment