I have a little side project running that allows me to play cards with a friend over the internet. I’m constantly fixing this or that and he has to re-download it and do some setup every time. I’ve never written an Auto Updater and thought it would be a fun weekend project.



Caveats:

doesn’t handle large files well, though I think it could be added without much effort (I don’t need it for this project)

uses the new Zip Archive from C# 4.5, had to add references to System.IO.Compression and System.IO.Compression.FileSystem, though don’t need your game to have this version

it doesn’t use HTTPS and so you could have a man in the middle attack, this is extra bad because you are running an exe file from the download (I didn’t fix this because you have to pay yearly for an SSL Certificate to do so)

The code is comprised of 3 parts:

the Uploader is a separate project that zips up the game and puts it on my server, handling some version number files that are used by the other parts

the Version Check is code that goes in the game startup before anything else happens, it launches the Updater if necessary

the Updater is a separate project that downloads and unpacks the new version from the server, and re-launches the game

Uploader – standalone

Whenever I’m happy with a new version of the game I run the Uploader and the server gets a zip file of the game that contains its version number, as well as an easy to access file with the version number.

Version Check – runs when starting up the game

I only run it if the debugger is not attached so that it doesn’t try to check for updates while I’m in development (there’s probably a better way of doing this with settings files). The updater code (below) is a program that I copy pasted to the debug directory of the game, so when you package up the game the updater goes as well. Then when we need to update we actually clone the updater and run the cloned version so that it’s possible to overwrite the updater program. If you wanted to get fancy you could have the Uploader copy the new Updater code when making the zip file.

Updater – standalone

I added the try catch because I struggled to find a good way of debugging this code as there’s a catch in that the Root directory is actually that of the Game and not the Update program folder because of how the process was started.

One problem with this current code is that any large files will have to get re-uploaded and downloaded every time as a part of the zip. If you have lots of large files that change you would need a better way of upload and download them separately.

Please send me an email or hit me up on twitter if you found this helpful.