Unless you’re using the ClickOnce deployment method for your applications, user settings are usually not transferred when updating to a new version of a C# desktop application. There’s in fact a quick and simple way to always keep the settings between versions.

The trick is to call the .upgrade() method your app settings object. The system will look for previous versions of your application in the App Data directory and copy the user settings to the new version automatically.

Properties.Settings.Default.Upgrade();

But now we have a problem. How do you know when the upgrade method should be called? As we don’t want to upgrade everytime the application is started, but only the first time after an upgrade. The solution is to create a new user settings variable that will be used to determine whether an upgrade is required. You can either make it a string and save the current application version, or make it a boolean and set it to True by default. I personally find the boolean method easier to use and this is the one I will explain below:

In your application load function (private void Window_Loaded in WPF), simply use this code:

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

It is important to place this code before anything else that use the user settings in the load method. In the above example, UpdateSettings is the user setting variable I created and set to True by default. When the application launch, it will check whether the UpdateSettings variable is set to True. If it’s the case, it will perform an upgrade. We then set the variable to False so that it doesn’t perform an upgrade the next time. Finally, we save the new settings. Why does it work? Because we set the user setting to True by default which will only occur after a new installation of the application.

Enjoy!