C# – Self updating application …

Disclaimer: I wrote this code way back in 2007 and I reckon I’ve learnt a fair bit since then (not being a professional developer).  Go easy on me if you feel like commenting on the code itself.  ðŸ™‚

This one might be interesting for people who have wondered about how to distribute the latest version of their application without having to tell people when an update is released.

There are built-in options that you can use with the Microsoft .NET platform such as ClickOnce. We use ClickOnce here at Trade Me to deploy some of our in-house applications and it works really well.

Recently, though, I thought “Why not see if I can write my own method of updating my application?” It turned out to be quite an interesting litte project! The application I was working on at the time is written in C# although the learning curve to go from VB.NET to C# or vice-versa isn’t too steep.

Where do we start?

To begin with I decided the update process should have the following steps.

  • Check my website for a text file containing version information.
  • Read the version information text file and compare the version information in it to the version of the application currently running.
  • If the version in the text file is the same or less than the version currently being run, do nothing other than show a message to the user.
  • If the version in the text file is greater than the version currently being run ask the user if they want to update to the later version.
  • Do the actual update.

Components

There are 2 components to this particular solution. The first part is the actual application being updated and the updater application. If you are going to follow along with any of the stuff in this article you’ll need to start a new solution with 2 projects. My main project is called “CountDown” and the updater application is called “AppStart” (for reasons not related to the updating stuff here). For the purposes of this article I’m going to assume you have a single project that needs the update functionality.

The main application

The first thing your main application will need to do is make use of the System.Net namespace by adding the following line to the top of the main namespace file.

Namespaces:

using System.Net;

When the user runs the application they can click a button that runs a method to do most of the steps above. The first thing the method does is try to resolve the IP address of my website (http://www.digitalformula.net resolves to 203.194.159.161).

Resolve IP Address:

try
{
  string RemoteDomain = "http://www.digitalformula.net";
  IPHostEntry inetServer = Dns.GetHostEntry(RemoteDomain.Replace("http://", String.Empty));
}
catch (Exception ex)
{
...
}