Question Update a WinForms-Application automatically (async)


New member
Apr 19, 2021
Programming Experience
Hello friends :)

I am trying to write a little application and, as I am only a student, i was wondering if someone maybe could help me with with some advices / tips to achieve the following:

On start, the desired program shall call a special function. That function looks onto a remote server (most likely it will be a NAS - network attached storage - with given username + password) if there is an update available. If yes, download & install it (including: display of the current progress for the user) and restart program afterwards.

Having done some research, I found the following code snippet, hope it helps:
var webClient = new WebClient();

webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

webClient.DownloadFileAsync(" Update/file.txt", "C:\\file.txt");

Please note: there is a blank character between KlingGastro and Update.
Furthermore the credentials are "KlingGastro" as username, please excuse that I will not post password due to security reasons. Lets say its "myPassword".

I think downloading is not going to be that big problem, I already found this tutorial: How to download a webfile with C# and show download progress synchronously and asynchronously
Rather the cancellation of the program by itself and the restart might could be difficult?

Sorry for all potential language faults in addition, hope it was nevertheless somehow understandable.
Would be really happy for every answer and help effort.

Best regards
Hello jmcilhinney and thank you so much for your answer,

as a program needs to be designed right from the sketch to support "ClickOne" and unfortunately the program I am trying to write that update functionality for wasn't - it is an existing WinForms (.NET-Version: 4.8) application grown over a long period of time, which can't be rewritten to support "ClickOne" due to pure size.
There's is nothing to do to use ClickOnce. You simply publish your app in VS and it will generate a ClickOnce installer. Automatic updates are configured in the project properties, not in code. The only reason that you'd need to write code is if you want to perform manual updates and, even then, it's a few lines. The idea that you can't write your app to support ClickOnce due to its size is completely false.
Yes you say it: "You simply publish your app in VS and it will generate a ClickOnce installer." That program I am talking about was written and published long time ago, it cannot be republished. I talked as well to the colleague who wrote that existing big program, he said analogous something like "The (target) application needs to be designed and published for this (meaning ClickOnce), otherwise it won't work" (I translated it from German, hope I hit the fitting vocab). That's why I said "write the app to support", sorry for wrong terminology, I hope you got now how I meant it^^ Please excuse my language difficulties.
@jmcilhinney : I suspect that app as written may have some hardcoded paths, requires running elevated, needs to play with the registry, or needs to reach out to files outside it data directory -- basically didn't follow best practices. Also, there's some other things that can't be done from within a ClickOnce application, but my memory of them is kind of fuzzy right now.

Anyway the usual trick for the restart is that you put in a scheduled task to run your application, or to run a batch file which will in turn restart your application. You can look in MSDN on how to do this scheduling.

As alternative, you could have a stub executable that lives parallel to your primary executable. All that stub does is actually perform the download and update, then run the primary executable. So all your primary app does when it determines updates are available, and the user consents to an update, is to launch the stub, and terminate itself.
Top Bottom