Tuesday 22 April 2014

Auto-update clients .NET applications

Project duration: 2 – 3 hours
Requirements: FTP server/Cloud storage

Recently I got bored of copying all the exe files through remote desktop to that few clients of mine that own my applications. I thought that it might be a smart idea to let them know about new version of their application that was just published. So they can directly upgrade without any sign of communication just as professional software does.

In my mind I already had mind model of the little window that will pop up each time new version is available to annoy hell out of them until they finally upgrade. With details what is being upgraded, fixed or extended in new version. Yes, great but how to do that - what are the options?

ClickOnce


Fully automatized tool from Microsoft as a build-in extension in Visual Studio 2012, pretty handy tool, in which you don’t need to take care about a lot of code. Basically what you do is set the path to your FTP or normal server, configure rest of the conditions and let the ClickOnce do the magic with deleting and replacing.

I would truly like to use this tool but not this time I need also to update my database structure – add/edit column properties, create stored procedures, a lot of work around SQL.

NetSparkle

          NetSparkle is a opensource software that has been reviewed by 4 people and downloaded by 6425 (by the time of writing).

No support for DropBox or other Cloud storage – This way I won’t need any FTP servers to download the file(s) from – potentially the best way for people who don’t have a domain with FTP server registered. (Can be mixed also with DO IT MYSELF solution). 

Do it myself

Guide that helped me so much to realize my vision was published on YouTube as a series named C# Auto Updater Library by guy who calls himself BetterCode. It would be stupid from me to just copy his work here with my explanations so here is the link.

Basic algorithm:
  1. Store an XML file on FTP server with information about new version, details of current version, MD5 hash, link to exe download.
  2. Download XML and check whether node “version” is newer than currently running application.
  3. If so follow the node “download link” to get exe as temporary file into client’s computer.
  4. Use background worker and cmd to do the replace and turn app on again work for you.


Project implementing/coding:
  1.  You need to have 2 separate projects in your .NET solution. First represents your main application distributed the clients second is an extension and library for auto updating that would contain mostly classes, interface and forms.
  2. Place XML containing newest version file on FTP server.
  3. Check current client’s app version versus FTP version.
  4. Download the exe file from link embedded in node of XML file, store it as temporary file.
  5. Get other child nodes containing SQL Database scripts e.g.
  6. When having everything downloaded from XML/FTP you will run this amazing code in cmd which is the only one that I will mention here from BetterCode and explain
/C Choice /C Y /N /D Y /T 4 & Del /F /Q \"{0}\" & Choice /C Y /N /D Y /T 2 & Move /Y \"{1}\" \"{2}\" & Start \"\" /D \"{3}\" \"{4}\"


  • Choice - accepts keyboard input to a batch file
  • deletes the old file in specified path {0}
  • waits 4 seconds
  • copies file from temporary files {1} to the specified path {2} 
  • waits 2 seconds
  • runs the file {4} in specified directory {3}
Parameters:

  • {0} - currentPath
  • {1} - tempFilePath 
  • {2} - newPath 
  • {3} - Path.GetDirectoryName(newPath) 
  • {4} - Path.GetFileName(newPath)


Visit http://ss64.com/nt/ for command line arguments information.


Setting the scene

In the time you are reading this blog you probably have gone through all of the boring parts of learning the syntax and very basics of programming and if not and you are a beginner that is looking for guide/tutorials for programming in this case I would recommend you to just keep searching on through the internet until you find tutorials that are comprehensible and friendly for your style of learning. There are several ways how to start learning programming.

  • books
  • tutorials/videos
  • learn by observing the code

I will try to post here as many articles as I can to let you know what I'm currently working on - with snippets of latest codes, explanations, short guides. I will be mentioning ideas that amazed me and current trends. 

What I find fascinating about programming?


For me programming means thinking with consequences - realizing aftermaths of your acts, thinking deep into the particular issue - analyzing the requirements, understanding the problem, finding its solutions and the Know What You Are Doing attitude really drives me. 

Or in some cases we just write few lines of code then hit the Debug button, cross fingers and start guessing what would happen when the debugger finishes building of our late night thoughts, Will it work? Do I need to ask this on SO? Usually it ends up with - oh no! just another missing ; syntax error..

Honestly now, mostly I like programming because it challenges me. It pushes me to the edges it tells me - "hey you moron, you cannot code this part without knowing how sockets and the network programming works". Programming knowledge never ends but it could end you when you become too lazy to read and study about new trends. 

Programming gave me the lifelong attitude not to sacrifice myself with only the nice shell of a thing I like or I promote. I just love to know what is inside the shell how and why it works this way - to understand the whole issue not just the cover.