📜 ⬆️ ⬇️

Automatic update programs in C #. Part 2

A few days ago I wrote an article about the implementation of automatic software updates in C #.

Taking into account the constructive criticism of commentators, it was decided to improve that code, adding a few new features, including improving the “old” ones:

In order not to reprint the text of the previous article , in this I focus only on the revised parts of the code.


Key Notes


Commentators of the previous article identified the following code flaws (indicating only those that relied on):

There were, of course, other comments similar to these, but I didn’t give them all, and I propose to begin the consideration of the problems.
')

Amendment


Since there may be some error when downloading the file, as a result of which the file will be damaged, the following corrections were made to the code:

public void checkUpdates(){ try { if (File.Exists("launcher.update") && new Version(FileVersionInfo.GetVersionInfo("launcher.update").FileVersion) > new Version(Application.ProductVersion)) { Process.Start("updater.exe", "launcher.update \"" + Process.GetCurrentProcess().ProcessName + "\""); Process.GetCurrentProcess().CloseMainWindow(); } else { if (File.Exists("launcher.update")) { File.Delete("launcher.update"); } Download(); } } catch (Exception) { if (File.Exists("launcher.update")) { File.Delete("launcher.update"); } Download(); } } 

First, we check if the launcher.update update file exists, and also check the version of the file, since we do not care about its extension. If the file is damaged, the try catch exception exception handler will be executed by executing the code to remove the damaged file and then running the check and download function (if found) for updates on the site.
If the file turns out to be whole and its version is higher than the current one, then the additional utility updater.exe is launched to carry out operations to replace the main program file. I will not elaborate in more detail, since it was already early .

Thus, we will verify the integrity of the update file.

 private void Download() { try { XmlDocument doc = new XmlDocument(); doc.Load(@"http://mysite/version.xml"); remoteVersion = new Version(doc.GetElementsByTagName("version")[0].InnerText); localVersion = new Version(Application.ProductVersion); if (localVersion < remoteVersion) { if (File.Exists("launcher.update")) { File.Delete("launcher.update"); } WebClient client = new WebClient(); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); client.DownloadFileAsync(new Uri(@"http://mysite/launcher.exe"), "launcher.update"); } } catch (Exception) { } } 

Further, the function of the file version verification process has changed, the key point of which is the implementation of file version comparison using the built-in System.Version tools, as a result of which the problem of correct version checking, say, “9.12.2” and “10.0.0” has been resolved.
In the case when a newer version is detected, the actions take place in the following scenario: the program automatically downloads the update file in the background, then gives the user a message about the availability of this update and offers 2 options for further developments:
  1. By agreeing to update , the program is instantly restarted, taking all necessary actions;
  2. Refusing to update the program stores the file next to the executable file, where it was downloaded. In this embodiment, the update process will occur the next time the program is started without displaying any notifications.


Conclusion


The rewritten code turned out to be more perfect compared to the previous version, and also excludes the possibility of applying the update on the “bit” file, as well as the lack of implementation of the checksum function of the downloaded file with the version on the server.

And I would like to say special thanks to the following people for their constructive criticism: DarkByte , naum , iroln , eyeless_watcher , teleavtomatika , wire .

Sincerely, Andrei Helldar!

PS: good people from among the minus - please kindly write in the comments why you decided so. It is interesting to know where I am wrong.

UPD. We kindly ask, who else has thoughts about “hand curves”, “curve code”, etc., write at least in the comments what is wrong. Based on your constructive criticism, I will improve my work, thereby learning how to write better code.
Thanks in advance!

Source: https://habr.com/ru/post/217633/


All Articles