How to upgrade application version and not loose user application settings
If you have been using or considering using new application settings feature in VS 2005 you might be wondering how not to loose user settings when you increment assembly version. The thing is that user settings are not automatically upgraded and you loose them if you don't do anything. Note that you loose only user settings while application settings are stored in app.config (or web.config) and are always there. The answer has two parts:
- If you use ClickOnce deployment it will take care of upgrading.
- If you don't use ClickOnce then you have to call Update method of ApplicationSettingsBase derived class (that is normally called Settings and created for you by the IDE). The acutall call would be something like
But how do you know when to call the Update method? This is a trick I learned from a blogger I can't remember right now, I think he is a MS employee (sorry):
- Define a user settings called i.e. UpdateRequired of type bool (the easiest way is through IDE). Make sure its default value is true.
- At the start of the application type this code:
Settings.Default.UpdateRequired = false;
So the Update will be called only after the version changes and only one time (since you disable further updates by setting UpdateRequired = false until version changes - when the property regains default value of true).
Have fun, application settings are worth exploring.