Automatic Updates
Posted: Wed Sep 02, 2009 12:37 am
Hello,
I am making this tutorial because it was a feature requested by a user. This tutorial will let you add a new function to your application that will allow to automatically detect application updates and then download and install them automatically.
Before I begin I have to tell you that in order for this to work you do need a webhost because its pretty much the only way I know of that can allow you to do automatic updates. But dont worry if you dont have a webhost because you can use google sites which is free and I have tested it with this tutorial and it all works great cooll; . So sign up for a google sites account and then you can continue with the rest of the tutorial.
All set up?, great lets get started.
This part of the tutorial is the bit that you need to add into the application you want to update automatically. Go into Project > Application Properties > Settings and add the following setting and its value
version - value = ver1
ver1 means that this is version 1 of your application. When you make an updated version of your application you need to change that value to ver2 and so on and so on for every new version you make.
Make sure you have these imports at the top of your code:
Imports System.Net
Imports System.Net.WebClient
Imports System.IO
Now in your applications startup event you need to add this code rite at the very top before anything else:
Start a new project and call it "UpdateChecker".
Now add a ProgressBar control to the form and a Timer control. Set the timer interval to "500" and leave it disabled. You can customize the form as you wish to match the look of your main application if you want but I recommend you keep the form small because its only for updating your application and will only be on screen for a few moments.
Now once you have added those controls you can simply override all the project code with the code below:
Dim url As String = "http://www.YOURSITE.com/updatedownload.html"
And you need to change the word "MyApplicationName.exe" to the name of your application, this occurs 5 times so you need to change them all. Once you are happy you have done everything just save and build the project and copy the "UpdateChecker.exe" into your main applications folder.
OK thats all the coding done so save>build and close VB. Now we need to make 2 files. The first file is called "version.html" and inside this file you need to type ver1 and upload it to your webhost. This file tells your application if it is the latest version or not.
Now whenever you make an update to your application you first need to change the text inside "version.html" to the next version number for example ver2. Then you need to upload your new updated application to your webhost and create the next file....
Second file is "updatedownload.html" and inside this file you need to put the download link to your updated application, so inside this file you would write something like "http://www.YOURSITE.com/MyApplication.exe" and then upload it to your webhost.
You can download the UpdateChecker source-code here if you want: If you need any help please let me know, I tried to explain it as clearly as possible but it does sound a little bit confusing especially the files part at the end. If you get stuck please let me know and I will help you.
Happy coding cooll;
I am making this tutorial because it was a feature requested by a user. This tutorial will let you add a new function to your application that will allow to automatically detect application updates and then download and install them automatically.
Before I begin I have to tell you that in order for this to work you do need a webhost because its pretty much the only way I know of that can allow you to do automatic updates. But dont worry if you dont have a webhost because you can use google sites which is free and I have tested it with this tutorial and it all works great cooll; . So sign up for a google sites account and then you can continue with the rest of the tutorial.
All set up?, great lets get started.
This part of the tutorial is the bit that you need to add into the application you want to update automatically. Go into Project > Application Properties > Settings and add the following setting and its value
version - value = ver1
ver1 means that this is version 1 of your application. When you make an updated version of your application you need to change that value to ver2 and so on and so on for every new version you make.
Make sure you have these imports at the top of your code:
Imports System.Net
Imports System.Net.WebClient
Imports System.IO
Now in your applications startup event you need to add this code rite at the very top before anything else:
Code: Select all
Thats all you have to do to enable it into your application. Now we move onto a completely new project, its realy short dont worry.Dim url As String = "http://www.YOURSITE.com/version.html"
Dim pageRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
Dim pageResponse As WebResponse = pageRequest.GetResponse()
Dim page As String = ""
Using r As New StreamReader(pageResponse.GetResponseStream())
page = r.ReadToEnd()
End Using
If Not page = Convert.ToString(My.Settings.version) Then
System.Diagnostics.Process.Start(Application.StartupPath & "\UpdateChecker.exe")
Me.Close()
End If
End Sub
Start a new project and call it "UpdateChecker".
Now add a ProgressBar control to the form and a Timer control. Set the timer interval to "500" and leave it disabled. You can customize the form as you wish to match the look of your main application if you want but I recommend you keep the form small because its only for updating your application and will only be on screen for a few moments.
Now once you have added those controls you can simply override all the project code with the code below:
Code: Select all
There are a couple of things you need to change in here so I will explains those to you now. You need to change the following URL to your websites URL:Imports System.Net.webclient
Imports System.IO
Imports System.net
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim url As String = "http://www.YOURSITE.com/updatedownload.html"
Dim pageRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
Dim pageResponse As WebResponse = pageRequest.GetResponse()
Dim page As String = ""
Using r As New StreamReader(pageResponse.GetResponseStream())
page = r.ReadToEnd()
End Using
Dim WebUpdate As System.Net.WebClient
WebUpdate = New System.Net.WebClient()
AddHandler WebUpdate.DownloadProgressChanged, AddressOf OnDownloadProgressChanged
AddHandler WebUpdate.DownloadFileCompleted, AddressOf OnFileDownloadCompleted
WebUpdate.DownloadFileAsync(New Uri(page), Application.StartupPath & "\Updated.exe")
End Sub
Private Sub OnDownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
Dim totalSize As Long = e.TotalBytesToReceive
Dim downloadedBytes As Long = e.BytesReceived
Dim percentage As Integer = e.ProgressPercentage
ProgressBar1.Increment(percentage)
End Sub
Private Sub OnFileDownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
If e.Cancelled Then
ElseIf Not e.Error Is Nothing Then
MsgBox("There was a problem downloading the update, please try again later.")
Else
If My.Computer.FileSystem.FileExists(Application.StartupPath & "\MyApplicationName.exe") Then
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\MyApplicationName.exe", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
End If
If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Updated.exe") Then
My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Updated.exe", "MyApplicationName.exe")
End If
Timer1.Enabled = True
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If My.Computer.FileSystem.FileExists(Application.StartupPath & "\MyApplicationName.exe") Then
System.Diagnostics.Process.Start(Application.StartupPath & "\MyApplicationName.exe")
Me.Close()
End If
End Sub
End Class
Dim url As String = "http://www.YOURSITE.com/updatedownload.html"
And you need to change the word "MyApplicationName.exe" to the name of your application, this occurs 5 times so you need to change them all. Once you are happy you have done everything just save and build the project and copy the "UpdateChecker.exe" into your main applications folder.
OK thats all the coding done so save>build and close VB. Now we need to make 2 files. The first file is called "version.html" and inside this file you need to type ver1 and upload it to your webhost. This file tells your application if it is the latest version or not.
Now whenever you make an update to your application you first need to change the text inside "version.html" to the next version number for example ver2. Then you need to upload your new updated application to your webhost and create the next file....
Second file is "updatedownload.html" and inside this file you need to put the download link to your updated application, so inside this file you would write something like "http://www.YOURSITE.com/MyApplication.exe" and then upload it to your webhost.
You can download the UpdateChecker source-code here if you want: If you need any help please let me know, I tried to explain it as clearly as possible but it does sound a little bit confusing especially the files part at the end. If you get stuck please let me know and I will help you.
Happy coding cooll;