Post: [VB.NET]Auto Updating
12-12-2010, 10:43 PM #1
Default Avatar
Oneup
Guest
(adsbygoogle = window.adsbygoogle || []).push({}); Now I'm sure there are more then 1 way to do this (and possibly easier ways) but this is just one way I figured out how to update software with just a text file.

Ive used it for a few projects and did notice some problems with the code, of which I will also go over so you don't end up with a program hanging when you goto use this yourself.

First of all I'll be going over multi-threading and reading a file from the internet and then compare it to what you are currently using.


To get us started:

If you are using a webserver simply making a text file called versionnumber.txt (or call it whatever you want, you'll just need to remember to change the code later on)


We need to set our imports:

    
Imports System.Net
Imports System.IO
Imports System.Threading


Our module level variable:

    Private WithEvents _Downloader As WebFileDownloader


Now we are going to make a Sub routine so we can call this at anytime down the road if we need it.
    
Public Sub Update()
Dim prodVersion As String = System.Text.Encoding.ASCII.GetString((webclient.DownloadData("https://www.arenagamerz.com/downloads/launcher/versionNumber.txt")))
Dim updatelink As String = "https://www.arenagamerz.com/downloads/launcher/arenagamerzLauncher.exe"
Dim UpdateLocation As String = My.Computer.FileSystem.CurrentDirectory

'Updater
If Me.ProductVersion() = prodVersion Then

Else
MsgBox("New Update available. Downloading")
Try


Try
_Downloader = New WebFileDownloader
_Downloader.DownloadFileWithProgress(updatelink, UpdateLocation & GetFileNameFromURL(updatelink))
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
Catch ex As Exception

End Try

End If
End Sub


This code (when called) will look for the specified filename on whatever server you are trying to get to. In this case we are looking for:
"https://www.arenagamerz.com/downloads/launcher/versionNumber.txt"

Updatelocation is where the updated client will be placed (you can make that anywhere you want, for the sake of this post I just made it the directory where this program is , which will cause a problem with overwritting)

Now there is a piece missing from this which is the function for getfilenamefromURL which will look like this:

    Private Function GetFileNameFromURL(ByVal URL As String) As String
If URL.IndexOf("/"c) = -1 Then Return String.Empty

Return "\" & URL.Substring(URL.LastIndexOf("/"c) + 1)
End Function


This is what is used to get the file name to download.

If you were to do this in FormLoad the program would hang because it's going out to the net to see if the initial file exists, if it doesn't it will take a long time to error out and the form won't appear for a while. So to make it so the form will appear and still do the check we are going to put this Sub in a thread.

      
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Start a new thread for the update Sub
Dim updateApp As Thread
updateApp = New Thread(AddressOf Me.Update)
updateApp.Start()


And that's all there is to it

The following user thanked Oneup for this useful post:

One

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo