58286

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

Question:

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?

Answer1:

Markus Olsson has already given a pretty good answer <a href="https://stackoverflow.com/questions/534261/how-do-you-keep-user-config-settings-across-different-assembly-versions-in-net/534335#534335" rel="nofollow">here</a>.

Essentially you will need to use the <a href="http://msdn.microsoft.com/en-us/library/system.configuration.applicationsettingsbase.upgrade.aspx" rel="nofollow">ApplicationSettingsBase.Upgrade()</a> method.

Answer2:

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).

Answer3:

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.

Recommend

  • FullTextHibernateSessionProxy is not visible from class loader
  • applying background to first, and every third row in a dynamically generated table
  • How to debug office add-in for mac?
  • Force iPad app to launch in portrait when landscape is supported
  • How many percent of the tweets does twitter sample API give?
  • c# linear regression given 2 sets of data
  • Can I use jquery to blank textarea fields or ajax like input boxes?
  • JPA CascadeType.REMOVE not working
  • Segmentation Fault on MySQL2 / Ruby 1.9.3 / Rails 3.2
  • Does CSS support inheritance [duplicate]
  • Retaining data after updating application
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • What causes the runtime difference in this trivial fortran code?
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • How to handle elastic beanstalk deployment so it uploads only changed files
  • Eclipse MTJ doesn't see Java ME SDK 3.0 devices
  • Filter strings with regex before casting to numeric
  • JBoss External Properties Files in Classpath
  • What and where is mdimport
  • How to define custom class, title, and target in Link Browser for content elements and the new rte_c
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Mysterious problem with floating point in LISP - time axis generation
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Java applet as stand-alone Windows application?
  • Join two tables and save into third-sql
  • Deserializing XML into class C#
  • Convert array of 8 bytes to signed long in C++
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Circular dependency while pushing http interceptor
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • How to include full .NET prerequisite for Wix Burn installer
  • Understanding cpu registers
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • costura.fody for a dll that references another dll
  • UserPrincipal.Current returns apppool on IIS
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize