I just wanted to run through a quick tutorial on how I check to see if there is an update available for an application of mine. I suppose it’s less of a tutorial and more of a code share. I shoot for asynchronous methods all the time. You’ll have two options, depending on which framework you’re targeting.
.NET Framework 4.0
private void CheckForUpdates()
{
// Checking for an update...
string str = "";
Task.Factory.StartNew(() =>
{
try
{
using (var client = new WebClient())
str = client.DownloadString("PASTEBIN URL");
}
catch (WebException)
{
// Failed checking for an update.
return;
}
}).ContinueWith(task =>
{
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
var downloadVersion = Version.Parse(str);
if (assemblyVersion < downloadVersion)
// An update is available!
else
// The application is up to date!
});
}
[details=.NET Framework 4.5]This framework introduces a new function to the WebClient class which allows us to make use of the async and await keywords.
private async void CheckForUpdates()
{
// Checking for an update...
string str = "";
try
{
using (var client = new WebClient())
str = await client.DownloadStringTaskAsync("PASTEBIN URL");
}
catch (WebException)
{
// Failed checking for an update.
return;
}
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
var downloadVersion = Version.Parse(str);
if (assemblyVersion < downloadVersion)
// An update is available!
else
// The application is up to date!
}
[/details]
Unless you have a server on which you can host a text file of the most recent version, I would use Pastebin because you can have a static URL hosting raw data despite changing the data itself. For example, this paste is what I use to host the most recent version and that URL is where I download the string from.
Of course, this doesn’t actually handle updating. I just redirect the user to the download page and let them take care of everything. But if you find a better way, share it here!