Tuesday, September 25, 2007

A simple 'Check for updates'

My brother asks when I'll post something interesting on here, but it hasn't happened yet so I doubt it will anytime soon... Anyway it seems the most boring posts are the most popular so I'll keep on posting crap.

Back to the point of this post. Lots of applications these days will check for an update when they start or when you press a 'Check for updates' button or menu item. Some really annoying applications have some little helper process that runs continuously and checks on a regular basis then throws up a big dialog telling you that there's an update (that's you Apple).

Not wanting to be left out, I thought I'd do the same. But being incredibly lazy, I couldn't be bothered to implement it completely. So this little bit of code will read a text file stored on the web server and tell the user that an update is available. It won't do anything fancy like download it for them or install it but I might add that at some point. Usage is simple, set the Url property to tell the component where the text file is stored, then call CheckLatestVersion() to show a message saying there is a new version available.

 

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Windows.Forms;

namespace FreeFlowAdministrator
{
    /// <summary>
    /// Component to check for updates for an application.
    /// </summary>
    public class UpdateChecker : System.ComponentModel.Component
    {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public UpdateChecker(System.ComponentModel.IContainer container)
    {
      ///
      /// Required for Windows.Forms Class Composition Designer support
      ///
      container.Add(this);
      InitializeComponent();
    }

    public UpdateChecker()
    {
      ///
      /// Required for Windows.Forms Class Composition Designer support
      ///
      InitializeComponent();
    }

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }


    #region Component Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      components = new System.ComponentModel.Container();
    }
    #endregion

    private string url;
    public string Url
    {
      get
      {
        return url;
      }
      set
      {
        url = value;
      }
    }

    public string GetLatestVersion()
    {
      HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);

      // Sends the HttpWebRequest and waits for the response.
      HttpWebResponse myHttpWebResponse = myReq.GetResponse() as HttpWebResponse;
      try
      {
        Stream response = myHttpWebResponse.GetResponseStream();
        StreamReader readStream = new StreamReader(response, System.Text.Encoding.GetEncoding("utf-8"));
        return readStream.ReadToEnd();
      }
      finally
      {
        // Releases the resources of the response.
        myHttpWebResponse.Close();
      }
    }

    public void CheckLatestVersion()
    {
      string latestVersion = GetLatestVersion();
      string currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
      if (latestVersion == currentVersion)
        MessageBox.Show("You are using the latest version");
      else
        MessageBox.Show(string.Format("The latest available version is {0}\nYou are using version {1}", latestVersion, currentVersion));
    }
  }
}

No comments: