On Properties.Settings.Default.Upgrade();

At some point in the last few years, the ability to upgrade one's Windows app's config file broke. Previously, this code would perform the upgrade:

if (Properties.Settings.Default.UpgradeRequired)
  {
      Properties.Settings.Default.Upgrade();
      Properties.Settings.Default.UpgradeRequired = false;
      Properties.Settings.Default.Save();
  }

For some reason, though, this no longer works, and hasn't for some time. Which means you really have to roll your own implementation.

What I did was create two methods for XML file handling. One saves the current options to disk, and another loads them. I plug the one that saves into the app's form's closing method, and I insert the other into the code I just showed you.

if (Properties.Settings.Default.UpgradeRequired)
  {
      UpdateOptions();
      Properties.Settings.Default.UpgradeRequired = false;
      Properties.Settings.Default.Save();
  }
When I call UpdateOptions(), it loads the latest options from disk and then sets the app's properties to those options before saving again using Properties.Settings.Default.Save().

The net result is every time the app closes, a copy of the app's options are saved to disk. And every time a new version of the app start's up, since Properties.Settings.Default.UpgradeRequired will be true (when you add this Boolean setting to project, you set it's default value to True), you'll update the options to those stored in the XML file and then re-save them, thus preserving the previous versions options.

And when I say options, I means settings. Microsoft called them settings, and they are typically stored in the config file, but in my case they are program options.

Comments