When we create new settings at runtime, add to Properties collection and try to save it, the new settings do not actually get saved in the user.config file.
I faced the same issue and fixed it as follow:
- Create a dummy property in settings at design time: This will ensure that the user.config file will be created and settings would be saved in it.
Now when we call Properties.Settings.Default.Save(); the default provider "LocalFileSettingsProvider" will save all the Properties in Settings.Default.Properties collections including the one added on runtime.
You will see that the new properties added to the collection at runtime are getting saved in the user.config file. But there is a second issue here which is when try to load the values from the user.config.
It will only load the value of the settings which are present at design time irrespective of other properties present in the user.config.
The fix I did for same is to add the property with some default value in the Properties collection again and then call following:
2 Load the property values from user.config:
SettingsProvider provider =
Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
SettingsPropertyValueCollection propertyValueCollection = provider.GetPropertyValues(
Properties.Settings.Default.Context, Properties.Settings.Default.Properties);
This will load the value from the user.config into the PropertyValues collection and then those values can be used.
Hope it helps...