How to persist changes in a .settings/.config file across a file version change?

C#.Netsettings.settings

C# Problem Overview


I have created an application that uses settings.settings to store some user specific settings (scope=User). Settings are loaded correctly on startup, changed during use and saved correctly for next launch. This cycle appears to have no problems.

The problem arises when I update the assembly and file versions for a new build. The settings are no longer loaded on startup (instead the default values are used). It also appears that a config file saved from version 1.1 will persist even if version 1.2 is launched and a NEW config file is generated and saved too (i.e. you can relaunch version 1.1 and the config file will be the config file that was saved from that version).

So it appears that the settings are specific to the version of the assembly and/or file. It is also worth noting that between version 1.1 and version 1.2 there were no changes to the settings.settings file or anything else for that matter (i.e. the only change I made between these different builds was modifying the version numbers).

Is there a way to persist these settings across version changes?

C# Solutions


Solution 1 - C#

A few clarifications:

You have to call the Upgrade method of ApplicationSettingsBase derived class (that is normally called Settings and is created for you by Visual Studio):

Properties.Settings.Default.Upgrade();

When/where to call the Upgrade method? There is a simple trick you can apply: define a user setting called UpgradeRequired (example) as bool (the easiest way is through IDE). Make sure its default value is true.

Insert this code snipped at the start of the application:

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

So the Upgrade method will be called only after the version changes and only one time (since you disable further upgrades by setting UpgradeRequired = false until a version change - when the property regains default value of true).

Solution 2 - C#

Markus Olsson has already given a pretty good answer here.

Essentially you will need to use the ApplicationSettingsBase.Upgrade() method.

Solution 3 - C#

I hope someone else has a better answer. I had this question a few years ago, and the only solution I could find (which did work) was to use my own mechanism for storing settings, rather than the default built-in .NET way.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionInvertedAccelerationView Question on Stackoverflow
Solution 1 - C#splattneView Answer on Stackoverflow
Solution 2 - C#VoidDwellerView Answer on Stackoverflow
Solution 3 - C#Andy JacobsView Answer on Stackoverflow