How to persist changes in a .settings/.config file across a file version change?
Solution 1:
A few clarifications:
You have to call the Upgrade
method of ApplicationSettingsBase
derived class (that is normally called Settings
and is created for you by Visual Studio):
Properties.Settings.Default.Upgrade();
When/where to call the Upgrade
method? There is a simple trick you can apply: define a user setting called UpgradeRequired
(example) as bool
(the easiest way is through IDE). Make sure its default value is true
.
Insert this code snipped at the start of the application:
if (Properties.Settings.Default.UpgradeRequired)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
}
So the Upgrade method will be called only after the version changes and only one time (since you disable further upgrades by setting UpgradeRequired = false
until a version change - when the property regains default value of true
).
Solution 2:
Markus Olsson has already given a pretty good answer here.
Essentially you will need to use the ApplicationSettingsBase.Upgrade() method.
Solution 3:
I hope someone else has a better answer. I had this question a few years ago, and the only solution I could find (which did work) was to use my own mechanism for storing settings, rather than the default built-in .NET way.