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:
In your program's
Main function, before it does anything else, insert the following code:
Updater.GitHubRepo = "/codewitch-honey-crisis/AutoUpdate";
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:
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,
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 is your repo name and
22.214.171.124 is the assembly version contained in the zip.
See the sample app's releases page for what this looks like in the real world.
- 19th August, 2019 - Initial submission