[VB.NET]Auto Updating
#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 Smile
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

[Image: H4JQn0Z.gif]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)