Showing posts with label programming. Show all posts
Showing posts with label programming. 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.

Monday, 5 November 2012

Database Designer Tool

I am a very visual person. I like to see animations in a UI. I even have blogged about liking the new UI from Microsoft's latest Windows installment, why because everything is there and it's visually easy.

So that leads me on to my methodology to database design. I have mentioned before that I like to use PGSQL. Recently I have had some work to do that required MySQL. However, the database design was done already and I was touching up the code for someone else. So I needed to see the database design in a visual way. I tried to make diagrams, but when we are talking about hundreds of database tables, views and other goodies, I was just wasting time, or at least it felt like it.

So I looked online for Database designers for MySQL. I found an excellent helping from a company called Micro Olap. They have a couple of products for various database types. The ones of interest to me were the Postgres and the MySQL database designers. Including (and this was very important for me) a trial of their software.

So I downloaded the MySQL trial and within 5 minutes I had a working diagram of the hundreds of tables. How? The software included a reverse engineer feature that worked on the database that I was using. It had a lot of options but I quickly found what I wanted and ignored the rest so the best thing to do would be to have a look for yourself.

Then when it appeared you have a huge area that you can move about and see the data tables and views in relation to one another. It is similar to the database designer that you get with Visual Studio but for more useful database engines.

After a small amount of time I felt proficient enough with the software to say that I enjoyed using it. Whilst it may not be the sort of thing that you are after, especially if you prefer to see your data in a less visual way (there are people still like that out there!) but for me it answered my problem for me and I didn't have to write out all the tables by hand (which was a bonus because years of computer use have left my writing hand as out of shape as retired dosser!)

The pricing is as follows (for the products that I was interested in ):
$124.95
MicroOLAP Database Designer for MySQL (Personal/Educational license)

$124.95
MicroOLAP Database Designer for PostgreSQL (Personal/Educational license)


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, 3 July 2012

Equals or ==?

So Android is pretty ubiquitous now. New tablets/phones coming out with the version 4 upgrades and awesomeness. I personally love my Android phone. No, it's not expensive, but I love it!

However, this is where I get to moan. I have never really touched Java before Android came out. Some people swear by it, I swear at it! It's not that Java is an aweful language, in fact there are some really nice features. However, from a C# point of view it is somewhat like learning Polish after learning Russian.

You see the slavic languages are very similar. The have a similar verb conjugation and many similar verbs, and then you get to the important bit, the verb that is the difference between like and love and it lets you down and changes. (Russian love transliterated is "lubit" in Polish lubic is to like).

The same thing happened with Java. I'm feeling confidant in the fact that I can write code without looking up syntax just because it's SO similar to C#. So much so that when i evaluated two variables with a

     if(var1 == var2) { // Do Something... }

statement, saw in debugging that they had the same value I expected that the // Do Something... code would run. WRONG! Turns out that the == in Java is COMPLETELY different to C#!

"EEEJIT!" you say. "Java is a better and older language than C#"

"No need to be rude!" Say I "C/C++ is an even older an more widely known langauge than even Java"

So how do we get around this evilness? Why does this happen?

Java-Samples states : "the equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance"

So the equals() method was what I needed. Tbh I had not been so dissapointed by a language inconsistancy since the lack of switch in python. I look forward to comments and suggestions (clean ones please) about what I'm missing in Java that makes it better than either C# and/or Python. 

Sunday, 1 April 2012

Welcome to my blog

Hi all. This blog is going to be all about programming and other aspects of life that are touched by computer use. I am not a programming master and as such I often come across new things that I learn in my travels. As I do so I will share them here and you all can get involved too.

One thing that I feel passionate about in the sphere of programming is the place of Open source and community software. Therefore, if I produce a tool, library or other set of code that I think will be useful, I will definately share it on either sourceforge or github (or both if I am feeling energetic.)

Contact me for more information or for a full CV!