Welcome to RightHand's community place Sign in | | Help

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.

Published 9. december 2005 23:08 by Miha Markic
Filed under:

Comments

Anonymous comments are disabled