Saturday, 3 August 2013

Writing a silent updater in c#

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. As always, if you see a security/efficiency issue, please let me know either in the comments or you can contact us direct:

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("" + new_version.Replace(".", "-") + "", 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).

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. 
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.

Sunday, 21 April 2013

Graphic Design Using Scribus

So this is a little different from what I usually blog about but here goes...

Recently I have been tasked with a new type of work for me. I usually enjoy programming, I cope with web design as this has a little programming from time to time, especially when you get into Javascript or PHP. I actually am looking forward to a Django project that I am going to get in to soon, but that is for another time.

However, a week or so ago, a contact of mine got in touch about a client that wanted a magazine to be designed for them. They are good at the acquisition of advertising and content, but not so hot on the design parts. I agreed as the money is constant and not half bad.

However, I am not the world's best graphic designer. It isn't one of my best qualities and I will be the first to admit that. I am no artist and my enjoyment with web designs isn't in HTML/CSS but in Javascript and server side scripting. Nevertheless, I gave it a good shot.

I did copious amounts of research and I found that there are really two pieces of software that you need in order to do the graphic design for a mag. First, a publishing software, second an image manipulator. The latter is catered for in GIMP, which I use daily anyway and I actually don't mind ( although at the risk of starting a holy war I am not saying it is better than any others, it's just what I am used to). The former was something that I needed to look at. So, what GOOD publishing software can you find.

Well, I narrowed it down further to 4 basic options. They are listed below with a little synopsis:

Adobe InDesign
Adobe products are generally first in any design conversation. People really love Photoshop for image manipulation, and I can't blame them, it does a lot of the leg work for you. I have heard great things about illustrator, and I am sure that for vector based images it is great ( I rarely do this so I don't really know how this program works ). One cannot ignore Dreamweaver either, although I can honestly say that I haven't ever created anything in it as it is a little too magic for my likes.

So InDesign is of a similar ilk. It is a fully featured program that does things for you and is easy to use, although you may need some time set aside to really work out how it works in order to become proficient on it. The thing is, as with MOST Adobe products, you will spend the time wisely as you can do all that you need to do on this program.

The other benefit is that it integrates with the other professional standard programs that we have already mentioned.

The con is as always the price. If this job turns in to doing this sort of thing more often, then I will go for this, if not then I will stick with the cheaper options.

This is another program that gets a lot of great reviews from professional users. The website is clean and the software works well. However, there is one problem that I have with this program and it is similarly reflected in the above option. The price! As far as I can tell this was THE publishing software to have when you were creating mags/books etc back in the day, but then, as always, other software came along. This program HAS I believe moved with the times, but the price is still so high that you would need to be a dedicated fan not to try out the cheaper (although not by much) and more integrated InDesign.

Microsoft Publisher
If you create a professional looking magazine in this software, please comment below and tell us how much you sold your soul to the devil for. This is not a horrible software if you need to do something quick and dirty but I cannot see how you could possibly use this to do the "real" work on.

This is an open source software and I should add, the one that I used in the end to get the first edition of the magazine up and running. The software has an adequate reference online and a reasonable interface that allowed me to work through the problems that I came across as an amateur.

There are some REALLY annoying features in Scribus that perhaps I came across as a result of using more than the others and therefore not finding the niggles in the others. One really bad feature is the text box not showing a preview of what the text will look like and being immensely buggy.

However, you over look that sort of thing when you CAN create a professional looking magazine in the time frame and you don't have to pay the earth, or even anything to use the software.

So I am annoyed with things in Scribus and it isn't the quickest way to do the job, but it is the cheapest and until the ground opens up with jobs of this nature, I will most likely stick with it. I appreciate your comments/suggestions as this is really a new area to me!