Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

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:
daniel@casserlyprogramming.com

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.

Wednesday, 31 October 2012

Windows 8 Now that I'm using it

So it's the biggest release of Windows in my computer history. I don't really remember the leap to Windows 95, so I can't really comment on how that changed my life for better/worse. However, the leap from Vista I only remember from the smug XP sidelines, I successfully avoided Vista (although annoyingly my mother has recently inherited a Vista laptop that runs slower than a sleeping snail), and felt thrilled and excited when my hard drive crashed which gave me an excuse to move to 7. So why did I move to 8 and how have I found it?

I moved over to 8 for a couple of reasons. Firstly, I want to develop for the new metro UI. That might seem strange, but I actually quite like the neat side scrolling action, and the fact that XAML has a use outside of winforms replacement.

Secondly, I love the price of Windows 8. I got the £25 ish update from windows 7 and that is just too good. (especially since you get the pro version).

Finally, my wife said she liked the design, and that was the last straw.

So how do I get on with Windows 8. The first thing to note is that the installation and upgrade allowed me to keep all of my files and most of my applications. A few needed re-installing (such as postgres server, synergy and other service based applications). This suited me as I have not long done a full reformat so there isn't much bloat on my PC anyway. With this option selected though, it is a whole evening of your life that you are never getting back (I think Windows owes me several of those already!!).

Next up was the change in the way things work. So the new metro interface is basically a large, fullscreen Start Menu. View it like this and things don't get as bad. I use the keyboard a lot in normal computer use so using "Win Key" for opening the menu or "Win Key" + "C" for the "Charms" menu isn't a hardship for me, although I can imagine this being annoying for mousers.

The whole OS has the feel of being better on a tablet, however, this by no means stops you from using the operating system on a desktop PC. I have two monitors and this is as usual much more useful, but has it's downsides with Windows 8. The charm menu can be a nightmare to get up on the left hand monitor!

The metro UI apps are a spot unusual. you have to either ALT + F4 to close or you return to the home screen in order to "hide" them. The idea is that all of your most useful apps are "in-memory" so that you can boot them up quicker next time. The problem with this is two-fold. Firstly, in memory means that you have to have the memory, although, tbh I haven't noticed this being a problem as of yet. Secondly, to close an app, you have to either mouse to the left edge, right click on the app and then choose to close it, or do it from the new look task manager. This can be rather frustrating if you need to close an app down, there just isn't a close button in site!

Other than these few annoyances and differences, the hype about how difficult it will be to adapt is greatly exaggerated. My wife, who is no computer expert, has been using it for 1 day and already doesn't need my help to use the same apps that she was using before. My opinion is that the desktop now almost has the same feel to it that the latest Ubuntu releases have with the HUD feature, which means that even if you are coming from a non-windows background, you should find plenty familiar with the setup.

Would I recommend Windows 8 to anyone? Yes but with the condition that you keep your mind open and don't rely on other's opinions.


Tuesday, 8 May 2012

Ubuntu 12.04 First Thoughts and synergy fix.

As I am rather an impulsive sort of person I thought that I would give an opinion on Ubuntu 12.04 only a few days after upgrading my server machine. (Server as in I keep my important files that I share with other machines, not as in I server apache websites!)

So my first thought? Well I was really excited by the idea of the HUD - not just because it seems like a term from a gaming interface, but also because the idea means we can escape EVER bringing a space hogging ribbon bar over to ubuntu (result!!). However, to note that Libre office (a default Ubuntu app) does not support this beast makes me want to cry in my cornflakes. ;-( However, the dream is there for future releases.

So the next thing I looked at was the black background that it gave me? Why? I had used one of the default wallpapers in the previous release of Ubuntu. A lovely autumnal walkway reminiscent of a parisienne path. However, on the new update, my favorite wallpaper has sadly been extinguished! With no real replacement amongst the defaults (and the fear of losing it again next release) I decided to use a picture. At least that is easy in Ubuntu!

Now today, and the reason for sharing this article, I'd had enough of clicking ctrl, stopping synergy, starting synergy and then going through the whole process next time i need the ctrl, shift or alt keys. Apparently the latest version of Ubuntu is incompatible with the stable release of synergy.

Google to the rescue and it appears that the Beta is fine. So I installed direct from the website on the Linux machine. At this point it must be noted that I am a moron, especially as when I tried to run it I nearly threw the cat across the room (a plan foiled due only to a lack of feline in the flat) when it still did not work. Then the moment of eureka when I remembered that the Windows machine would most probably need updating too.

Now as updates to beta versions of products go, I am yet to find anything that I do not like and particularly on the Windows version of Synergy. User interface is easier than going deaf in a night club and I was back up and running in no time. What's more, the ctrl button works!!!

So, what do I think of Ubuntu 12.04? For me it has caused numerous, small headaches. However, I LOVE Ubuntu. I was talking to collegue in the office the other day who said that he could not wait to get home to work on Ubuntu again (the office is nearly all Windows 7/XP). He could not stop thinking about it! Whilst he might just be the saddest person I know, I share his sentiments about using Linux in general and the rush that it gives you. Furthermore, the user base and community (aside from bearded zealots) are superb and what's more, IT'S FREE!

So the big question is, will I ever do all of my work on Ubuntu? hmmmn, probably not anytime soon, C# is still mainly a Visual Studio beast, despite the advancements of Mono. Although, given time and incentive (this mainly means when Windows 8 is released and I can't take any more metro, ribbon bar in the explorer, which is by the way a complete break of Microsoft's own usage terms for the Ribbon bar and HUGE BUTTONS that are useless not on a mobile device after being hit with a hammer on the fingers.......) I might just move over to the lovely Linux for good. Here's to the future!