ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!
Home
Upgrade
Credits
Help
Search
Awards
Achievements
 10259

[VB.NET]Auto Updating

by 1UP - 10-11-2016 - 03:53 PM
#1
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:

Code:
Imports System.Net
Imports System.IO
Imports System.Threading

Our module level variable:

Code:
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.
Code:
Public Sub Update()
        Dim prodVersion As String = System.Text.Encoding.ASCII.GetString((webclient.DownloadData("http://www.arenagamerz.com/downloads/launcher/versionNumber.txt")))
        Dim updatelink As String = "http://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:
"http://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:

Code:
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.

Code:
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
Reply
#2
Very nice, thank you for sharing man. Heart
Are you a guest to this site? Click the image below and sign up today!
[Image: FTYbRmR.gif]
Reply
#3
Thank you for sharing this excellent Tutorial, will come into use for me personally one day :)
Reply
#4
Nice share, but seems one of the more over complicated for a simple outcome type of scripts. I may be making a basic copy and paste code for simple newbs. :yus:
Jacked by Red @ https://pulses.xyz/
Reply
#5
A better idea is to download a zip file and use param 16 i believe to overwrite files. Also to kill any processes that are currently running that you are updating (assuming this program is external from the one you are updating). Other than that, clean code and nice tutorial. I hope people can learn from this.
Reply
#6
Great tutorial!

The way I like to auto update is, I download the bytes of said program I want to updated store it in a memory stream. Then I verify it's integrity(I have various ways of doing this) and then I cache the old one in case an error occurs and finally write bytes to disk.

I could post some code if you'd like.
Reply
#7
great tut will def come in handy for someone
Reply
#8
thanks for sharing :) i know how to do auto update in C# but not VB so thank u!
Reply

Users browsing: 4 Guest(s)