Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Check For Updates - RemoteVersionChecker

0.00/5 (No votes)
22 Mar 2012 2  
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here