Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Tip/Trick

Check For Updates - RemoteVersionChecker

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Mar 2012CPOL 8.7K   169   16  
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

C#
// 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:

XML
<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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Italy Italy
Creator of:
Impulse Media Player http://impulsemediaplayer.codeplex.com
Audio Pitch & Shift http://audiops.codeplex.com
Ultimate Music Tagger http://umtagger.codeplex.com
Modern Log Viewer http://modernlogviewer.codeplex.com
Pitch Tuner http://pitchtuner.codeplex.com
Modern Audio Tagger http://modernaudiotagger.codeplex.com
Win Log Inspector http://windowsloganalyzer.com/win-log-inspector/
Win Log Analyzer http://windowsloganalyzer.com/win-log-analyzer/

Comments and Discussions

 
-- There are no messages in this forum --