how to Make Cleint / App / EXE/ Visual Basic Auto Update With Script
Introduction:
This tutorial is on how to create an auto update function for a program in Visual Basic.
Notes:
- You will need a website for the latest version to be hosted for download and checking. Or, for testing you can use localhost like I am (I'm using XAMPP).
- If you're using a localhost, only people on your network can use this auto update function.
Steps of Creation:
Before we start we want to create a new form with one button for the update process to begin. we also want to import:
Imports System.IO Imports System.Net
Step 1:
First we want to get the latest version of the program available which is hosted in a version.txt file on our website/localhost. Let's get the source of that file and compare it to our programs current version...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim src As String = getSrc("http://127.0.0.1/autoUpdater/version.txt") If (src.Contains(My.Settings.version)) Then MsgBox("Up to date!") End If End Sub Private Function getSrc(ByVal url As String) Dim r As httpwebrequest = httpwebrequest.create(url) Dim re As httpwebresponse = r.getresponse() Dim src As String = New streamreader(re.getresponsestream()).readtoend() Return src End Function
Step 3:
Now that we have the latest version available and have output "Up to date!" if the current programs version is up to date we can make it download the latest file if it is not up to date...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim src As String = getSrc("http://127.0.0.1/autoUpdater/version.txt") If (src.Contains(My.Settings.version)) Then MsgBox("Up to date!") Else MsgBox("Outdated, Downloading new exe...") Try My.Computer.Network.DownloadFile("http://127.0.0.1/autoUpdater/" & src.Replace(" ", "") & "/download.exe", CurDir() & "/updates/" & src & ".exe") MsgBox("Downloaded, Running...") Diagnostics.Process.Start(CurDir() & "/updates/" & src & ".exe") MsgBox("Finished!") Me.Close() Catch ex As Exception MsgBox("Updated file not found...") End Try End If End Sub
We surround the process in a try and catch statement just in case anything fails such as if the program isn't available to download.
Once we have downloaded the file and saved it we run it and close our current program. The final thing to do is to set the version of our program(s)...
Step 4:
To set a version setting for our program you want to go to:
Project > Project Properties > Settings, and create a new String named version and set the value to the current version of the program.
Project Complete!
That's it! Below is the source code to all the files, the directory formats and download to the program...
Auto Update Visual Basic Program:
Imports System.IO Imports System.Net Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim src As String = getSrc("http://127.0.0.1/autoUpdater/version.txt") If (src.Contains(My.Settings.version)) Then MsgBox("Up to date!") Else MsgBox("Outdated, Downloading new exe...") Try My.Computer.Network.DownloadFile("http://127.0.0.1/autoUpdater/" & src.Replace(" ", "") & "/download.exe", CurDir() & "/updates/" & src & ".exe") MsgBox("Downloaded, Running...") Diagnostics.Process.Start(CurDir() & "/updates/" & src & ".exe") MsgBox("Finished!") Me.Close() Catch ex As Exception MsgBox("Updated file not found...") End Try End If End Sub Private Function getSrc(ByVal url As String) Dim r As httpwebrequest = httpwebrequest.create(url) Dim re As httpwebresponse = r.getresponse() Dim src As String = New streamreader(re.getresponsestream()).readtoend() Return src End Function End Class
Version.txt:
0.2
Website Directory Format:
Root/version.txt in the root of the site which holds the latest version of the program available for download.
Root/0.2/download.exe Contains the downloadable.exe for the version 0.2 of my program ready for download.
If you have a version 2.0 just create the folder appropriately, like so:
Root/2.0/download.exe