65.9K
CodeProject is changing. Read more.
Home

Check For Updates - RemoteVersionChecker

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Mar 22, 2012

CPOL
viewsIcon

8900

downloadIcon

170

This code provides your application a - check for updates - feature

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

// main call
public static void CheckForUpdates(bool showMessageAnyway)
{
    //WebProxy webProxy = new WebProxy("foo.host", 8080);
    //webProxy.Credentials = new NetworkCredential("foo", "foo");

    WebProxy webProxy = new WebProxy();

    CheckForUpdates(showMessageAnyway, webProxy);
}  

// do the call using task allows to save you app from freezing 
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.