Nothing special, just whipped up a basic updater in my spare time since I haven’t coded for a while.
To use just place the xml file in the rar in %Temp% or change the local location of the file to the file on your website. (http://www.yourwebsite.com/ApplicationVersion.xml) I just set it as a local file to test it. Or you could just look at the code and learn from it.
There might be an easier way of doing it – but this way works.
Imports System.Xml
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckForUpdates()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBtn.Click
CheckForUpdates()
If AvailableVer.Text.EndsWith(CurrentVer.Text) Then
MsgBox("Current version is up to date", MsgBoxStyle.OkOnly + vbInformation, "Check for updates")
ElseIf AvailableVer.Text = "null" Then
If MsgBox("Could not connect to internet. Please check your connection and try again.", MsgBoxStyle.RetryCancel + MsgBoxStyle.Critical, "Check for updates") = MsgBoxResult.Retry Then
CheckForUpdates()
Else
Me.Close()
End If
Else
If MsgBox("Update available! Would you like to update now?", vbYesNo + MsgBoxStyle.Exclamation, "Check for updates") = MsgBoxResult.Yes Then
'Put what you want to happen now here when update is available
Else
Me.Close()
End If
End If
End Sub
Private Sub CheckForUpdates()
Try
Dim VersionFile As XDocument = XDocument.Load("C:\Users\" & System.Environment.UserName & "\AppData\Local\Temp" & "\ApplicationVersion.xml")
For Each line As XElement In VersionFile...<Application>
Dim version As String = line.Element("Version")
AvailableVer.Text = version
Next
Catch ex As Exception
AvailableVer.Text = "null"
End Try
If AvailableVer.Text = "null" Then
If MsgBox("Could not connect to internet. Please check your connection and try again.", MsgBoxStyle.RetryCancel + MsgBoxStyle.Critical, "Check for updates") = MsgBoxResult.Retry Then
CheckForUpdates()
Else
Me.Close()
End If
ElseIf AvailableVer.Text.EndsWith(CurrentVer.Text) = False Then
If MsgBox("Update available! Would you like to update now?", vbYesNo + MsgBoxStyle.Exclamation, "Check for updates") = MsgBoxResult.Yes Then
MsgBox("impulate update process")
Else
Me.Close()
End If
End If
End Sub
End Class
Here’s my adaptation of an application updater. The form can obviously be styled to look nicer and to feature more information, but this gives you the basics. I briefly explained some of the variables and methods to make it more clear. If anyone has any questions, just ask!
Source Code: [details=Open Me]
using System;
using System.Diagnostics;
using System.Net;
using System.Windows.Forms;
namespace AutoUpdater
{
public partial class frmMain : Form
{
// The current version of the application.
private string currentVersion = "1";
// The local path in which the current application is stored.
private string currentApplicationPath = "";
// The local path to which the updated application will be downloaded.
private string updatedApplicationPath = "";
// The URL to the file that will tell us the newest version.
private Uri versionFileUri = new Uri("");
// The URL to the newest file.
private Uri updatedApplicationUri = new Uri("");
public frmMain()
{
InitializeComponent();
}
// We will check for updates on load.
private void frmMain_Load(object sender, EventArgs e)
{
if (IsUpdateAvailable())
DownloadUpdate();
else
{
Process.Start(currentApplicationPath);
Application.Exit();
}
}
// Downloads the file that will tell us the newest version and compares
// it to the current version. Returns true if an update is available,
// and false if there is not.
private bool IsUpdateAvailable()
{
using (WebClient client = new WebClient())
{
string newestVersion = client.DownloadString(versionFileUri);
if (int.Parse(newestVersion) > int.Parse(currentVersion))
return true;
else
return false;
}
}
// Downloads the updated file to the specified local path.
private void DownloadUpdate()
{
using (WebClient client = new WebClient())
{
// When the download is complete, client_DownloadFileCompleted is run.
client.DownloadFileCompleted += client_DownloadFileCompleted;
// When the progress of the download changes, client_DownloadProgressChanged is run.
client.DownloadProgressChanged += client_DownloadProgressChanged;
// Downloads the updated file asychronously so we can track the progress
// as well as keep the application from being non-responsive.
client.DownloadFileAsync(updatedApplicationUri, updatedApplicationPath);
}
}
// Once the download is complete, we will run the updated file.
void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Process.Start(updatedApplicationPath);
Application.Exit();
}
// Changed the label text to show how many MBs there are left, and changes the
// progress bar value to the progress percentage of the download.
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
downloadInformation.Text = string.Format("{0} MB / {1} MB", e.BytesReceived / 1000000, e.TotalBytesToReceive / 1000000);
downloadProgress.Value = e.ProgressPercentage;
}
}
}
@Brettv
Try to update when current version is 1 and the available version is 1.1, don’t used EndsWith and prefribably don’t use strings here.
And XML… @Krossuryne
You might want to add checks for if your WebClient 404s, or other errors.
And BTW, there are 1048576 bytes not 1000000 in a MB.
You’re right! I made the mistake of assuming Google’s converter (when I googled megabyte to byte) was using the binary system and not the decimal system. Thanks for telling me though!