Upgrading user settings
Or: Why your saved settings are always overridden by the previous ones
In an application of mine I persist user settings (like username, form position, window state, etc.) in a user configuration file (for more information on this topic in general have a look at this MSDN article).
In code you can access this settings like this:
var user = Properties.Settings.Default.User;
Properties.Settings.Default provides also methods for saving changed settings and upgrading persisted settings from a previous application version.
What upgrade does is to copy the settings file from a path like “path-to-settings/Company/MyApplication/1.0.0.1/user.config” to the path reflecting the current application version: “path-to-settings/Company/MyApplication/1.0.0.2/user.config”.
The crux of the matter is that copying the file is always performed, never the less whether there is already a user configuration file for the current version or not.
In conclusion this means you as the programmer have to make sure that Upgrade is only called once after you upgraded to a new version. Otherwise all changed values are always replaced by the last state from the old application version.
An easy solution to circumvent the described behavior of the upgrade method is to check whether a property in the user settings has still its default value, although it should have been set already:
if (String.IsNullOrEmpty(Properties.Settings.Default.User)) Properties.Settings.Default.Upgrade();