0

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

  1. User Opens the App for the first time (e.g. v1.0)
  2. App creates user settings in default directory with default values
  3. User makes amendments to settings and closes the App
  4. App stores values in settings file (user.config)
  5. 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)

  1. User copies new version (e.g v1.1) of App to same directory and replaces old version.
  2. User Opens the new version (v1.1) for the first time
  3. App executes (and presumably creates a new user.config in a new folder of v1.1 with default values)
  4. App detects the settings need migration and performs migration of settings (move old stored settings from v1.0 to v1.1)
  5. 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.
  6. User Opens the App again and the old settings can be read as usual.

Ideal Migration behaviour

  1. User copies new version (e.g v1.1) of App to same directory and replaces old version.
  2. User Opens the new version (v1.1) for the first time
  3. App executes (and presumably creates a new user.config in a new folder of v1.1 with default values)
  4. App detects the settings need migration and performs migration of settings (move old stored settings from v1.0 to v1.1)
  5. App refreshes it’s runtime access to settings without the need to restart.
  6. User can read stored settings as usual.
gbdavid
  • 1,639
  • 18
  • 40
  • according to microsoft documentation whenever you update a property those values update themselves and if you want then persisted you have to use the save which you are doing. Just in case i am misreading the problem here, take a setting for example and tell me the value before running, the value after running and the value it should have. But as im saying, if you are directly consuming the settings they should be updated as soon as you change them – nalnpir Jan 25 '19 at 14:22
  • Thanks for the reply, yes, I'm using the .Save() across the application and the settings always persist ok. The problem I have is that if I build a new version, I will then replace the old version with the new one, and then at launch, I have to close and restart again so that the persisted settings are available. – gbdavid Jan 25 '19 at 15:07
  • So once you overwrite the app you have to open your new app one time and close it so you have your settings?? – nalnpir Jan 25 '19 at 15:21
  • Yes, that's what happens, if I have v1.0, then I replace with v1.1, I have to run v1.1 for the first time, then quit, then any future runs of v1.1. I'll have the old user settings restored as usual. – gbdavid Jan 25 '19 at 16:42
  • honestly im lost here, my bet would be that once you make the new version something changes in the config file, so when it first runs it writes over its own version or something but since you dont save the changes only the version number changes – nalnpir Jan 25 '19 at 17:19
  • No, the user.config file is automatically saved by .net in C:\Users\\AppData\Local\\. I believe that during migration what .net does is auto create a new folder with ..\ and put the default/empty user.settings there. Then I run migration, and I guess .net copies the user.settings from the older folder and moves it to the new folder.. But that's just my guess, I don't know why in that process things can't be refreshed at run time, and you have to quit the App and restart it so you can read the migrated settings... – gbdavid Jan 25 '19 at 17:52

0 Answers0