Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

AutoUpdate: A GitHub Enabled autoupdater

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
19 Aug 2019CPOL3 min read 19.1K   400   15   23
Automatically update your winforms app using github release binaries

Introduction

So it's nice to be able to automatically update your apps, and a lot of people use GitHub these days, so I thought, why not make a little autoupdater that can grab release binaries from GitHub to update itself with?

That's what this is. This little drop-in will take a GitHub repo, find the releases, look for a special tag "Refresh.vMajor.vMinor.vBuild.vRevision" with the highest number, and greater than the current assembly's version, and then grab the zip from that, download it, replace the binaries in the current running folder with the contents of the zip, and finally rerun the updated app with the same command line arguments passed to it originally.

Woo, that's a lot of churn, but this makes everything relatively automatic.

Disclaimer: It works with winform apps, but I can't necessarily recommend it for console apps because it will interrupt shell redirects and piping operations since it closes the app and reruns it.

Preparing the Application

Take the ZZupdate0.exe, add it to the root of your project as a file in your project, and then go the properties and choose "Embedded Resource" for the Build Action. This puts the updater into your project. The updater is a small bootstrapper to allow your app to close so your executable files and locked files can be replaced before it runs your app again.

You'll need to use the assembly version information so that the updater can deal with versioning. Make sure you always set your assembly version.

Add the Updater.cs file to your project and add the namespace to the top of Program.cs:

C#
using AutoUpdate;

In your program's Main function, before it does anything else, insert the following code:

C#
// setup the auto update, and exit if there was an update
// change the line below to your github repo where "AutoUpdate" is (YourAppName)/(YourRepoName)
Updater.GitHubRepo = "/codewitch-honey-crisis/AutoUpdate";
if (Updater.AutoUpdate(args))
  return;

Please don't do the following except for testing, but if you need to explicitly force an update to a specific version, you can call:

C#
Updater.Update(new Version(major,minor,bld,rev));

I've included the Updater shim in the sample as well. Visual Studio sometimes hiccups when trying to embed its output into the AutoUpdate sample, but just Rebuild again and the error should go away.

When running the sample, keep an eye on the version reported by the form. You might see the update window fly by briefly, but then you'll see the form pop back up with the asm version and the args it received from the command line so you can see the update happened. The args were whatever was passed through to the app initially.

Preparing the GitHub Repro and the Zips

These are the steps for adding an update release to GitHub. Please follow them precisely. Look to the enclosed sample for guidance as well:

This works best with a project already using GitHub for source control management, so probably make one of those.

Either way, you'll need a repo to draw from. Make one if you haven't above.

After you've made one, go to releases, and draft a new release.

The tag must be (case-sensitive) Refresh.v<AssemblyVersion>, so for example, Refresh.v1.0.0.5

The zip file must directly contain the contents of your binary folder, not the folder itself. The application's executable should be in the root directory of the zip. I just go to bin\Release and highlight everything but the .pdb and then go Send To|Compressed Folder...

Name the zipfile <GitRepoName>.Refresh.v<AssemblyVersion> so for example MyApp.Refresh.v1.0.0.5, if MyApp is your repo name and 1.0.0.5 is the assembly version contained in the zip.

See the sample app's releases page for what this looks like in the real world.

History

  • 19th August, 2019 - Initial submission

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Just a shiny lil monster. Casts spells in C++. Mostly harmless.

Comments and Discussions

 
QuestionA salt on the wound Pin
Member 1360328631-Aug-21 9:41
Member 1360328631-Aug-21 9:41 
AnswerRe: A salt on the wound Pin
honey the codewitch31-Aug-21 13:05
mvahoney the codewitch31-Aug-21 13:05 
GeneralRe: A salt on the wound Pin
Member 1360328631-Aug-21 22:36
Member 1360328631-Aug-21 22:36 
QuestionZZupdater00.exe Pin
Member 1360328631-Aug-21 9:41
Member 1360328631-Aug-21 9:41 
AnswerRe: ZZupdater00.exe Pin
honey the codewitch31-Aug-21 13:07
mvahoney the codewitch31-Aug-21 13:07 
GeneralRe: ZZupdater00.exe Pin
Member 1360328631-Aug-21 22:47
Member 1360328631-Aug-21 22:47 
QuestionCompatibility with Framework 4.8? Pin
Strilles29-May-21 4:52
Strilles29-May-21 4:52 
AnswerRe: Compatibility with Framework 4.8? Pin
honey the codewitch29-May-21 5:09
mvahoney the codewitch29-May-21 5:09 
GeneralRe: Compatibility with Framework 4.8? Pin
Strilles29-May-21 5:12
Strilles29-May-21 5:12 
GeneralRe: Compatibility with Framework 4.8? Pin
honey the codewitch29-May-21 5:17
mvahoney the codewitch29-May-21 5:17 
GeneralRe: Compatibility with Framework 4.8? Pin
Strilles29-May-21 5:56
Strilles29-May-21 5:56 
GeneralRe: Compatibility with Framework 4.8? Pin
honey the codewitch29-May-21 6:19
mvahoney the codewitch29-May-21 6:19 
GeneralRe: Compatibility with Framework 4.8? Pin
honey the codewitch29-May-21 5:33
mvahoney the codewitch29-May-21 5:33 
GeneralRe: Compatibility with Framework 4.8? Pin
Strilles29-May-21 5:36
Strilles29-May-21 5:36 
GeneralRe: Compatibility with Framework 4.8? Pin
honey the codewitch29-May-21 6:05
mvahoney the codewitch29-May-21 6:05 
GeneralRe: Compatibility with Framework 4.8? Pin
Strilles29-May-21 6:12
Strilles29-May-21 6:12 
GeneralRe: Compatibility with Framework 4.8? Pin
honey the codewitch29-May-21 6:26
mvahoney the codewitch29-May-21 6:26 
GeneralRe: Compatibility with Framework 4.8? Pin
honey the codewitch29-May-21 6:43
mvahoney the codewitch29-May-21 6:43 
GeneralRe: Compatibility with Framework 4.8? Pin
Strilles29-May-21 8:01
Strilles29-May-21 8:01 
GeneralRe: Compatibility with Framework 4.8? Pin
yonan223610-Jun-21 3:25
yonan223610-Jun-21 3:25 
GeneralRe: Compatibility with Framework 4.8? Pin
honey the codewitch10-Jun-21 4:07
mvahoney the codewitch10-Jun-21 4:07 
QuestionAutoUpdate: A GitHub Enabled autoupdater: difficulty performing the post Pin
morote16-Sep-19 19:55
morote16-Sep-19 19:55 
thanks in advance for the answer, I have difficulty understanding what values I have to put in :'GitHubRepo' e '/releases/latest' .
my remote folder e: https://www.miosito.it/download
my thread...MyApp.Refresh.v1.0.0.1.zip
whatever I do, either goes wrong or exits with return result = 0.

WebRequest wrq = WebRequest.Create(string.Concat("https://github.com", GitHubRepo, "/releases/latest"));

cordial greetings.
AnswerRe: AutoUpdate: A GitHub Enabled autoupdater: difficulty performing the post Pin
honey the codewitch11-Dec-19 17:42
mvahoney the codewitch11-Dec-19 17:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.