I am trying to add settings to Settings file dynamically. I use code below:
public PathSourceDir(int id)
{
InitializeComponent();
_id = id;
SettingsProperty property = new SettingsProperty(Properties.Settings.Default.Properties["baseSetting"]);
property.Name = "dynamicSettingName" + _id.ToString();
property.DefaultValue = _id.ToString();
Properties.Settings.Default.Properties.Add(property);
Properties.Settings.Default.Save();
//Read stored value
var dynamicSetting = Properties.Settings.Default["dynamicSettingName" + _id.ToString()];
}
Code runs, however if I comment code lines where new property is created and Settings are saved, and run again, I get an exception:
System.Configuration.SettingsPropertyNotFoundException: 'The settings property 'dynamicSettingName6' was not found.
Meaning that setting previously was not saved in the file. However code was executed before also with id=6. Could you please advise what is wrong?