[VB.Net] Auto Update Tutorial for VB.Net 2014



There are only 3 parts that needs changing and them parts are commented.
Ok first you need a webhost/website

Step 1

Make a webpage, call it UpdateCHK.html
Add this

<center>
Private page to check my programs for updates.
<br><br>

MyProgramName=v1.0=YOURDOMAINHERE/ProgramFileName.zip
<br><br>

</center>
Change this line to match yours "MyProgramName=v1.0=YOURDOMAINHERE/ProgramFileName.zip" I use .zip but you can use .exe if you want.

Step 2

In VB make an import at the very top
Imports System.Text.RegularExpressions

Step 3

Add these

Dim CurrentVersion As String = "v1.0" '--- Change this to Current version, needs changing on every update
Dim ProgramName As String = "MyProgramName" '--- Change this to Your Progran Name
Dim SiteName As String = "YOURDOMAINHERE/UpdateCHK.html" '--- Change this to Your Update page
Dim VersionCHK, GetVer, GetVerLink As String
Dim GetUpd As Integer

There is only 3 things to change that are commented for you.

Step 4

Add this full Sub

Public Sub AutoUpdate()
Dim WebRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(SiteName) 
Dim WebResponse As System.Net.HttpWebResponse = WebRequest.GetResponse 
Dim STR As System.IO.StreamReader = New System.IO.StreamReader(WebResponse.GetResponseStream()) 
Dim ReadSource As String = STR.ReadToEnd 
Dim Regex As New System.Text.RegularExpressions.Regex(ProgramName & "=v(\d+).(\d+)=(.*?).zip") 
Dim matches As MatchCollection = Regex.Matches(ReadSource)

For Each match As Match In matches 
Dim RegSplit() As String = Split(match.ToString, "=") 
GetVer = RegSplit(1) 
GetVerLink = RegSplit(2) 
Next

If GetVer > CurrentVersion Then 
GetUpd = MsgBox(ProgramName & " is an old version." & vbCrLf & "New Update is available" & _ 
vbCrLf & "Current version: " & CurrentVersion & vbCrLf & "Version Avalible: " & _ 
GetVer & vbCrLf & vbCrLf & "Update Now?", vbYesNo, "Update")

If GetUpd = vbYes Then 
Dim sfd As New SaveFileDialog 
sfd.FileName = IO.Path.GetFileName(GetVerLink) 
If sfd.ShowDialog = DialogResult.OK Then 
My.Computer.Network.DownloadFile(GetVerLink, sfd.FileName) 
End If 
End If
Else 
MsgBox(ProgramName & " is upto date." & vbCrLf & "Current version: " & CurrentVersion, 0, "Update") 
End If
End Sub

And thats it, call "AutoUpdate()" on either a button click or form load

I know there is shorter ways to do this like using webbrowsers ect but this is my method and it works like a charm :D

Thanks and Enjoy


Related Posts