Visual Basic Auto Update 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:
  1. Imports System.IO
  2. 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...
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.     Dim src As String = getSrc("http://127.0.0.1/autoUpdater/version.txt")
  3.     If (src.Contains(My.Settings.version)) Then
  4.         MsgBox("Up to date!")
  5.     End If
  6. End Sub
  7.        
  8. Private Function getSrc(ByVal url As String)
  9.     Dim r As httpwebrequest = httpwebrequest.create(url)
  10.     Dim re As httpwebresponse = r.getresponse()
  11.     Dim src As String = New streamreader(re.getresponsestream()).readtoend()
  12.     Return src
  13.   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...
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.     Dim src As String = getSrc("http://127.0.0.1/autoUpdater/version.txt")
  3.     If (src.Contains(My.Settings.version)) Then
  4.         MsgBox("Up to date!")
  5.         Else
  6.         MsgBox("Outdated, Downloading new exe...")
  7.         Try
  8.             My.Computer.Network.DownloadFile("http://127.0.0.1/autoUpdater/" & src.Replace(" ", "") & "/download.exe", CurDir() & "/updates/" & src & ".exe")
  9.             MsgBox("Downloaded, Running...")
  10.             Diagnostics.Process.Start(CurDir() & "/updates/" & src & ".exe")
  11.             MsgBox("Finished!")
  12.             Me.Close()
  13.         Catch ex As Exception
  14.             MsgBox("Updated file not found...")
  15.         End Try
  16.     End If
  17. 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:
  1. Imports System.IO
  2. Imports System.Net
  3. Public Class Form1
  4.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5.         Dim src As String = getSrc("http://127.0.0.1/autoUpdater/version.txt")
  6.         If (src.Contains(My.Settings.version)) Then
  7.             MsgBox("Up to date!")
  8.         Else
  9.             MsgBox("Outdated, Downloading new exe...")
  10.             Try
  11.                 My.Computer.Network.DownloadFile("http://127.0.0.1/autoUpdater/" & src.Replace(" ", "") & "/download.exe", CurDir() & "/updates/" & src & ".exe")
  12.                 MsgBox("Downloaded, Running...")
  13.                 Diagnostics.Process.Start(CurDir() & "/updates/" & src & ".exe")
  14.                 MsgBox("Finished!")
  15.                 Me.Close()
  16.             Catch ex As Exception
  17.                 MsgBox("Updated file not found...")
  18.             End Try
  19.         End If
  20.     End Sub
  21.  
  22.     Private Function getSrc(ByVal url As String)
  23.         Dim r As httpwebrequest = httpwebrequest.create(url)
  24.         Dim re As httpwebresponse = r.getresponse()
  25.         Dim src As String = New streamreader(re.getresponsestream()).readtoend()
  26.         Return src
  27.     End Function
  28. End Class
Version.txt:
  1. 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

Add new comment