-
-
Notifications
You must be signed in to change notification settings - Fork 40
Quick start
Create a simple WinForms application, make sure it targets .NET Framework 4.6 or higher. Put a TextBox
that will display the application's current version, call it VersionTextBox
. Also, add two buttons -- UpdateButton
and ExitButton
.
Add some code-behind:
Form1.cs
using System;
using System.Reflection;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Display application version
VersionTextBox.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
private void UpdateButton_Click(object sender, EventArgs e)
{
// Will add code later
}
private void ExitButton_Click(object sender, EventArgs e)
{
// Exit the application
Application.Exit();
}
}
}
Install Onova either through the UI or by typing Install-Package Onova
in the package manager console.
Note: this guide assumes you have the latest version of Onova installed.
In this example, we will be using LocalPackageResolver
which will look for application packages in a specified directory. We'll also be using ZipPackageExtractor
because our packages are going to be zip archives.
Create an empty directory somewhere (e.g. c:\Projects\MyApp\OnovaPackages\
), it will function as the package repository, where different packaged versions of our application will be stored.
Change the code-behind of your WinForms app so that it integrates with Onova now.
Form1.cs
using System;
using System.Reflection;
using System.Windows.Forms;
using Onova;
using Onova.Services;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private readonly IUpdateManager _updateManager = new UpdateManager(
new LocalPackageResolver("c:\\Projects\\MyApp\\OnovaPackages\\", "*.zip"),
new ZipPackageExtractor());
public Form1()
{
InitializeComponent();
// Display application version
VersionTextBox.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
private async void UpdateButton_Click(object sender, EventArgs e)
{
// Check for updates
var check = await _updateManager.CheckForUpdatesAsync();
// If there are none, notify user and return
if (!check.CanUpdate)
{
MessageBox.Show("There are no updates available.");
return;
}
// Prepare the latest update
await _updateManager.PrepareUpdateAsync(check.LastVersion);
// Launch updater and exit
_updateManager.LaunchUpdater(check.LastVersion);
Application.Exit();
}
private void ExitButton_Click(object sender, EventArgs e)
{
// Exit the application
Application.Exit();
}
}
}
The code we just added will check for updates and download them if they are available. After that, it will launch the external updater which will overwrite the application files and start the application again.
Compile your application and copy the binaries to some other folder. This will be our initial release.
If you launch the application now and press the update button, it will say that there are no updates available, because the package repository is empty.
Go back to your application and open AssemblyInfo.cs
located in Properties
in Solution Explorer. Change the AssemblyVersion
attribute value to 2.0.0.0
.
[assembly: AssemblyVersion("2.0.0.0")] // <---- change this
Build your application again, but this time put the binaries into a zip file called WinFormsApp-v2.0.zip
and move it to c:\Projects\MyApp\OnovaPackages
, the package repository directory. This way we created a new version of our application and made it available for Onova to find.
Open the initial version of the app, the text box should say the application version is 1.0.0.0
. If you press the update button now, it will find the package we just added and perform an update.