Introduction
This utility shows you how to implement a button that compares your application version with the last version available on your server.
Background
In order to do a check for a specific version of your app on the server, it is enough to put an XML containing nodes named latestversion
and optionally latestversionurl
within relative value.
Using the code
public static void CheckForUpdates(bool showMessageAnyway)
{
WebProxy webProxy = new WebProxy();
CheckForUpdates(showMessageAnyway, webProxy);
}
public static void CheckForUpdates(bool showMessageAnyway, WebProxy webProxy)
{
Task.Factory.StartNew(() => RemoteVersionChecker.CheckForUpdates(
UriDemoXml, webProxy)).ContinueWith(task =>
{
if (task.Result != null)
{
RemoteVersionChecker.VersionInfo lastVersionInfo = task.Result;
Version productVersion = GetProductVersion();
if (lastVersionInfo.LatestVersion > productVersion)
{
DialogResult result = MessageBox.Show(String.Format(
"A new version ({0}) is available, do you want to go to the homepage?",
lastVersionInfo.LatestVersion), "NEW VERSION",
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
Process.Start(lastVersionInfo.LatestVersionUrl);
}
}
else if (showMessageAnyway)
{
MessageBox.Show(String.Format("This version is up to date",
lastVersionInfo.LatestVersion), "No updates available",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
if (showMessageAnyway)
MessageBox.Show("Network error", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
});
}
The XML document should have this structure:
<versioninformation>
<latestversion>1.0.0</latestversion>
<latestversionurl>http://audiops.codeplex.com</latestversionurl>
</versioninformation>
Points of Interest
Using Task
, especially with MethodInvoker
when managing with GUI objects, is a simple and efficient way to call routines in a separate thread.