Post view page

Post #27

Upgrading Settings in WPF
2012-01-30 09:51:07

All application I write have to have a setting file which means they have to be upgraded.  Below is the basic code I stick in all my projects.

I add version number to setting and then check that against the version number in application and run the upgraded if needed.

In App.xaml.cs I add the follow to the OnStartup and OnExit override to update and save the settings.

Note that since by default the system does not add forms I add the System.Windows.Forms to the reference to see product version.

protected override void OnStartup(StartupEventArgs e)
{
    try
    {
        CoinAnalyzer.Properties.Settings.Default.Reload();
        if (CoinAnalyzer.Properties.Settings.Default.VersionNumber != System.Windows.Forms.Application.ProductVersion)
        {
            CoinAnalyzer.Properties.Settings.Default.Upgrade();
            CoinAnalyzer.Properties.Settings.Default.VersionNumber = System.Windows.Forms.Application.ProductVersion;
            CoinAnalyzer.Properties.Settings.Default.Save();
        }
    }
    catch (Exception ex)
    {
        TraceError("Error loading settings", TraceTypes.App, ex);
    }
   
    base.OnStartup(e);
}

protected override void OnExit(ExitEventArgs e)
{
    try
    {
        CoinAnalyzer.Properties.Settings.Default.Save();
    }
    catch (Exception ex)
    {
        TraceError("Error saving settings", TraceTypes.App, ex);
    }

    base.OnExit(e);
}

 

 

Test right content

first test

Test right content

second test

Test right content

third test

this is a test