[ACCEPTED]-Keep user's settings after altering assembly/file version-versioning

Accepted answer
Score: 59

Use the built in Settings classes, you just 13 need to upgrade the settings anytime you 12 change the application version. Here's 11 how to do it: In the Settings.settings file, create 10 a new setting UpdateSettings type=bool Scope=User 9 Value=True

Include the following code before 8 you use any Settings (it can run every time 7 the app runs, as this makes running in debugger 6 easier too)

// Copy user settings from previous application version if necessary
if (MyApp.Properties.Settings.Default.UpdateSettings)
    MyApp.Properties.Settings.Default.UpdateSettings = false;

When your new application version 5 is run UpdateSettings will have a default 4 value of True and none of your old settings 3 will be used. If UpdateSettings is true 2 we upgrade the settings from the old settings 1 and save then under the new app version.

Score: 5

Here's how I solved it.

In the GUI application 11 it is very easy to restore the settings 10 by executing

Properties.Settings.Default.Upgrade ();
Properties.Settings.Default.Reload ();
Properties.Settings.Default.NewVersionInstalled = false;
Properties.Settings.Default.Save ();

However, I've always had the 9 problem that all other libraries lost their 8 settings when a new version has been installed. With 7 the following implementation the software 6 runs through all assemblies of the AppDomain 5 and restores the settings of the respective 4 library:

foreach(var _Assembly in AppDomain.CurrentDomain.GetAssemblies())
    foreach(var _Type in _Assembly.GetTypes())
        if(_Type.Name == "Settings" && typeof(SettingsBase).IsAssignableFrom(_Type))
            var settings = (ApplicationSettingsBase)_Type.GetProperty("Default").GetValue(null, null);
            if(settings != null)

I've implemented the code in the 3 App.xaml.cs of the GUI project and it will 2 always be executed when the setting "NewVersionInstalled" was 1 set to true by a new version.

Hope this helps!

More Related questions