Maintaining Application Settings Across Versions

I recently stumbled upon an issue that, provided the correct circumstances could prove to be slightly troubling. Recently, I had to deal with multiple versions of a test project I'd been working on. I had to store a few things to be loaded at run-time, and saved on close. Doing this, I saved a few things to the project settings. However, upon changing the versions (installing an update) the settings were lost, as a new file was generated for the latest version. Perplexed, I turned to the internet. After a shockingly large amount of searching, I finally happened upon some code that helped.

First, I added an application version string to my settings file.

ApplicationSettings.Default.AppVersion;

Upon running any version of the application, a version comparison is run. If the version comparison showed the stored version, and the current application version were different, Settings.Upgrade() is used to bridge the gap between the two versions.

Example code placed in a method run at start up:
Found here.

 
// Get the current version of the application.
Version ver = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
// Get the old version of the application, the last one stored
string oldVersion = Settings.Default.AppVersion;
if(oldVersion != ver.ToString())
{
        Settings.Default.Upgrade();
        Settings.Default.AppVersion = ver.ToString();
        Settings.Default.Save();
}

See the MSDN reference for the Settings.Upgrade method here.,

Post new comment

The content of this field is kept private and will not be shown publicly.