Writing an auto updater – Casserly Programming

Writing an auto updater

  ·  Code  ·  No Comments

You Are Here:Writing an auto updater

So recently I have been working on some software for windows for a company here in the UK. The software does lots of cool stuff including taking orders and products from Quickbooks and syncing with a website.

One of the things that I wanted to do, was to avoid having to release an update individually to each user that has the software installed (this could be quite a few down the line). Therefore, I thought that I would write a simple auto-updater that works behind the scenes. In the making of it there were a number of new tricks that I learnt that I thought that I would share with the world.

How it works
The basic design was that a windows service would run and check regularly, whether or not the software was up to date. If it is not, then it downloads the latest version and overwrites the needed files. As this is a fairly small project without massive files (and is unlikely to be such), this approach is acheived in the following way:

  • The service has a timer. The timer runs every 5 mins.
  • If the software is open, we don’t do the update (point 1)
  • There is a version stored on a database that is accessible from the service. The local version is stored in an ini file. If the versions don’t match we perform the update.
  • We download a zip file which is the files that have changed since the last update (with the version number as the name of the zip) (point 2)
  • Then we unzip and overwrite the files in the installation directory of the program (the service exe is shipped in the same directory so that we can unzip the files straight to the root of the program. (point 3)
  • Then we update the local version number and log the update (I also send myself an email to state that <this computer name> has updated to <the version number>).
All that the software itself does is on load it checks if the service is installed. If not it installs it (which in turns starts the service). If it is installed it checks if it is running. If not it starts the service. Simples!
Point 1 – Checking that the software is running
I found some code that seems to work fairly well and finds if any Process is running:
  
public bool IsProcessOpen(string name) 
 {
      foreach (Process clsProcess in Process.GetProcesses())  
     {
        if (clsProcess.ProcessName == name)   
       {
          return true;   
       }
     }
     return false;
 }

Then I pass in the process name for my software to check that if it is running.

Point 2 – Downloading Files
This was something fairly new to me. However may come in handy. In order to do this (I presume there are multiple ways as usual) simply, the following code will help:

using (WebClient client = new WebClient())
{
    client.DownloadFile("http://urltozip.com/" + new_version.Replace(".", "-") + "-EXE.zip", new_fn);
}

Point 3 – Unzipping files
I found an excellent library for using standard zip files in c# (unlike python the standard zipping/compressing is not really that useful IMHO).
http://dotnetzip.codeplex.com/

using that all I needed to do was the following to get my files out of the zip:

using (ZipFile zip = ZipFile.Read(new_fn))
{ 
  foreach (ZipEntry ent in zip)
  {
    ent.Extract(".", ExtractExistingFileAction.OverwriteSilently);
  }
}

A few points that are important here:

  • The using is very important. We need to dispose of the ZipFile object (according to the readme).
  • The “.” is the directory we want to unzip to. In my requirements this was the same directory as the zip. An empty string will cause an error.
  • The second parameter is an enumeration. I chose the silent overwrite for obvious (I hope) reasons.
Conclusion
This only took me a few hours of coding and Googling. Therefore, we will see how it stands the test of time. I imagine that there are more elegant ways of doing a lot of this and this will work for my requirements much better than probably most other software.

Again, if you have any comments, please feel free to let me know.

About the author:

Daniel is the founder of Casserly Programming. He's worked in Software development for nearly a decade starting with web design and moving up to .NET and ASP.NET. In later years he has contracted and on the side been developing web services in Python and mobile applications.

Leave a Reply

Your email address will not be published. Required fields are marked *


Top