When a new version of my App is built, user settings automatically detect that an upgrade is required, I use the following to upgrade and persist the changes:
static bool UpgradeUserSettings()
{
if (Settings.Default.UpgradeRequired)
{
Settings.Default.Upgrade();
Settings.Default.UpgradeRequired = false;
Settings.Default.AppUpgradeOnLastRun = true;
Settings.Default.Save();
MessageBox.Show("The application has been upgraded from a previous version. You will need to restart the application.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return true;
}
return false;
}
I run this check at launch and just before I start the application - the UpgradeUserSettings()
is called within Initialise()
MyApplicationContext context = new MyApplicationContext();
var result = Initialise();
if (result)
Application.Exit();
else
Application.Run(context);
I had to implement it this way because if I run UpgradeUserSettings()
, and carry on in the same instance of the App, the App keeps reading the default values of the settings, rather than the older stored values that the user has. I even tried Settings.Default.Reload()
after migrating, but the App still reads the default values.
Is there any other way for the App to read the stored/persisted values after migrating the App without the need for the user to restart the App manually?
Note: My App is configured so that only one instance can run at any one time, so I can't code it to create a new instance of the App and close the current instance from code :(
Update - App workflows:
To clarify how things are working and what I would like to happen, these are the current App workflows (standard of any .NET app that implements basic user settings functionality)
Default Settings Behaviour - excluding any migration
- User Opens the App for the first time (e.g. v1.0)
- App creates user settings in default directory with default values
- User makes amendments to settings and closes the App
- App stores values in settings file (user.config)
- User Opens the App again and they can read stored settings.
Default Migration behaviour (Providing all the steps of default settings behaviour above happened already)
- User copies new version (e.g v1.1) of App to same directory and replaces old version.
- User Opens the new version (v1.1) for the first time
- App executes (and presumably creates a new user.config in a new folder of v1.1 with default values)
- App detects the settings need migration and performs migration of settings (move old stored settings from v1.0 to v1.1)
- After settings are migrated, the App does not have access to the new settings at runtime yet, so App needs to be closed and opened again, User closes the App.
- User Opens the App again and the old settings can be read as usual.
Ideal Migration behaviour
- User copies new version (e.g v1.1) of App to same directory and replaces old version.
- User Opens the new version (v1.1) for the first time
- App executes (and presumably creates a new user.config in a new folder of v1.1 with default values)
- App detects the settings need migration and performs migration of settings (move old stored settings from v1.0 to v1.1)
- App refreshes it’s runtime access to settings without the need to restart.
- User can read stored settings as usual.