C# Application settings

Had a play around with the Click once installer the other day and it seems quite straightforward to use. Instead of being installed in a particular directory, the application is installed within the user folders. It then has none of the rights problems when writing files. When a new version is installed, the application is put in a new path with some randomly generated guid folder name. This gives a problem when trying to take across previous versions of property settings. The following code will solve the problem quite simply.

I hadn’t realized it was as easy as this so I had a long piece of code to recognize where the app was before and to bring the values across. If the version is updated, the application will be looking for the saved settings in the wrong directory path, because the application GUID has changed. I had previously added code in my applications to find the previous directory path and then use that, but it is a bit verbose and I now see the correct solution.

Firstly, you will need to add a Property in the settings to show that the application version has changed. I call this ‘callUpdate’ and set its default value to true. When a new version of the application runs, it will check this value; if the directory has changed there will be no previous value found, so the default value will be set. So, in the start up of the application, we can test for a true value; if true, we call the Upgrade function to find the older version of the settings, update the values, then we save the value ‘callUpdate’ as false so that it will not be called next time.

I now replace my previous lengthy code, with a simple call to Upgrade, as shown below.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
bool updateForNewVersion = Properties.Settings.Default.callUpgrade;

if (updateForNewVersion)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.callUpgrade = false;
Properties.Settings.Default.Save();
}
txtIP.Text = Properties.Settings.Default.ipAddress;
// etc
}

Advertisements

~ by ianm on January 6, 2012.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: