Introduction
When you change the Assembly version, the next load from the configuration data will come back with default values - because the version has been changed and .NET isn't sure the config will be valid. For users, this is either annoying, or harmful depending on what information you choose to store. This Tip provides a simple solution to keeping existing configuration.
Using the code
Add a new Setting to your assembly, call it ApplicationSettingsVersion
, make it a string, leave it User, and leave the default value blank.
Paste the code into your main Form constructor, above any configuration access.
string thisVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
if (Properties.Settings.Default.ApplicationSettingsVersion != thisVersion)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.ApplicationSettingsVersion = thisVersion;
}
As the comments say: you can't move this to a utility assembly, and call the method, because you want to upgrade the settings for the main EXE file - and while you could use GetEntryAssembly
instead of GetExecutingAssembly
to get the correct Assembly, it would affect the wrong settings, as the DLL could have it's own, separate settings file - mine do for common utility look-and-feel.
You need to execute this code in each Assembly with it's own settings.
History
Original Version