How to upgrade application version and not loose user application settings

If you have been using or considering using new application settings feature in VS 2005 you might be wondering how not to loose user settings when you increment assembly version. The thing is that user settings are not automatically upgraded and you loose them if you don't do anything. Note that you loose only user settings while application settings are stored in app.config (or web.config) and are always there. The answer has two parts:

  1. If you use ClickOnce deployment it will take care of upgrading.
  2. If you don't use ClickOnce then you have to call Update method of ApplicationSettingsBase derived class (that is normally called Settings and created for you by the IDE). The acutall call would be something like

         Settings.Default.Update(); [C#]
         My.Settings.Update()   [VB]

But how do you know when to call the Update method? This is a trick I learned from a blogger I can't remember right now, I think he is a MS employee (sorry):

  1. Define a user settings called i.e. UpdateRequired of type bool (the easiest way is through IDE). Make sure its default value is true.
  2. At the start of the application type this code:

      if (Settings.Default.UpdateRequired)
      {
         Settings.Default.Update();
         Settings.Default.UpdateRequired = false;
      }

So the Update will be called only after the version changes and only one time (since you disable further updates by setting UpdateRequired = false until version changes - when the property regains default value of true).

Have fun, application settings are worth exploring.

Comments (3) -

  • Bill

    13.12.2005 18:03:42 | Reply

    I don't know how you foudn this -but totally excellent post!

  • Miha Markic

    14.12.2005 10:44:19 | Reply

    I was forced to look for a solution Smile

  • Desiderius

    6.8.2006 11:03:49 | Reply

    Thanks for the tip.  However it would seem that the code actually required at the start of the application is:

    if (Settings.UpdateRequired)
    {
        Settings.Upgrade();
        Settings.UpdateRequired = false;
    }

Loading