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/184.108.40.206/user.config” to the path reflecting the current application version: “path-to-settings/Company/MyApplication/220.127.116.11/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();
Was an early adopter and I developed bad habits years ago because silly work arrounds appearently were needed at that time. I found and read your little post. I realise and can confirm that it is actually as simple as You tell us (VS2005+VS2010+VS2012). It is safe to use also for persisted user defined datasets and even accepts schema changes gracefully.
Thank You very much for your tip, that this part of .Net is not anymore broken here.
Comments are closed, but trackbacks and pingbacks are open.