Writing my own Auto Updater

ASDFdotASPX picture ASDFdotASPX · Oct 30, 2008 · Viewed 12.9k times · Source

When writing my own auto updater, is there a general framework that I should be following?

A while ago I was reading up on how one should create a 'boot strapper' that will load first before the main application (since a running appilation can't be updated due to file locks etc.)

So any tips/best practices for this?

Answer

MusiGenesis picture MusiGenesis · Oct 30, 2008

You'll probably have to write your own. As FOR mentioned, the basic idea is to put the latest version of your program (I'm assuming an EXE) on the server, and then have your application check with the server when it starts, and download the EXE from the server if it's a newer version.

I've usually implemented this as a web service that the application calls at startup. A couple of warnings about this approach:

  1. The web service method needs to get the version number of the EXE on the server and compare it to the version number of the caller. If you use the Assembly class to read the version number of the server EXE, this will lock the file for as long as the web service instance is running (at least 20 minutes). As a result, you may sometimes have trouble replacing the EXE on the server with a newer version. Use the AssemblyName class instead - this allows you to read the assembly info without loading the assembly (and locking it).

  2. The caller application can't replace its own file with the new version - you can't delete or update a running application file. What it can do, however, is to rename its own file while running. So the trick on an auto-update is for the application to rename itself (e.g. "MyApplication.exe" to "MyApplication_OLD.exe"), download the new version into the application folder (named "MyApplication.exe"), notify the user that an update has occured which requires a restart of the application, and then end. When the user restarts the application, it will be the newer version that starts - this version checks for and deletes the old version.

Doing an auto-update that automatically restarts the application after an update like this is very tricky (it involves kicking off another process and then ending its own process before the auto-restart process kicks in). I've never had a user complain about having to restart the app.