0
\$\begingroup\$

I've created little block of code that should update a external program, but I've checked it through and have not seen possible error or warnings. There are no errors in my testing but I wan't to publish it so tried to make it as comfortable to many users as possible. I hope that someone could check it too and tell me how they could do some parts differently.

Public Module AutoUpdate
    Public Sub CheckUpdate()

        Dim HttpRequestLink As String
        'Change the 'yourlink' to your version text file link.
        HttpRequestLink = "yourlink"

        Dim OlderVersionMessageBox As String
        'Change the 'yourmessage' to your own message.
        OlderVersionMessageBox = "yourmessage"

        Dim UpdatePath As String
        'Change the 'yourpath' to update downlaod link.
        UpdatePath = "yourpath"

        Dim UpToDateMessageBox As String
        'Change the 'yourmessage' to your own message.
        UpToDateMessageBox = "yourmessage"

        Dim ApplicationProductName As String
        'Change the 'test.exe' to your own filename.
        ApplicationProductName = "test.exe"

        Dim RequestFunction As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(HttpRequestLink)
        Dim ResponseFunction As System.Net.HttpWebResponse = RequestFunction.GetResponse()
        Dim SR As System.IO.StreamReader = New System.IO.StreamReader(ResponseFunction.GetResponseStream())
        Dim NewestVersion As String = SR.ReadToEnd()
        Dim CurrentVersion As String = Application.ProductVersion
        Try
            If NewestVersion.Contains(CurrentVersion) Then
                MsgBox(UpToDateMessageBox, MsgBoxStyle.Information)
            Else

                Threading.Thread.Sleep(50)
                MsgBox(OlderVersionMessageBox & vbNewLine & "Current Version: " & CurrentVersion & vbNewLine & "Latest Version: " & NewestVersion, MsgBoxStyle.Information)
                Dim CurrentApplicationPath As String = Application.ExecutablePath
                Dim ApplicationName As String = (Application.ProductName)
                If System.IO.File.Exists(Application.StartupPath & "\" & ApplicationProductName) Then
                    System.IO.File.Delete(Application.StartupPath & "\" & ApplicationProductName)
                Else
                End If
                My.Computer.Network.DownloadFile(UpdatePath, Application.StartupPath & "\" & ApplicationProductName)
                MsgBox("Updated Succesfuly!" & vbNewLine & "Application will be closed in 5 seconds!", MsgBoxStyle.Information)
                Delay(5)
                Process.Start(Application.StartupPath & "\" & ApplicationProductName)
                Application.Exit()
            End If
        Catch ex As Exception
            Dim ErrorMessageBox As String
            ErrorMessageBox = "Unhandled Error: " & ex.Message & vbNewLine & "Time: " & My.Computer.Clock.LocalTime
            MsgBox(ErrorMessageBox, MsgBoxStyle.Critical,)
        End Try
    End Sub
    Sub Delay(ByVal dblSecs As Double)
        'Delay Function code by Ashkore Dracson https://www.youtube.com/channel/UCXh3Skub198wfTtJCQlR8cA
        'Thank You! <3
        Const OneSec As Double = 1.0# / (1440.0 * 60.0#)
        Dim dblWaitTil As Date
        Now.AddSeconds(OneSec)
        dblWaitTil = Now.AddSeconds(OneSec).AddSeconds(dblSecs)
        Do Until Now > dblWaitTil
            Application.DoEvents()
        Loop
    End Sub
End Module
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

You might consider having a simple text file that contains the latest version number. That way you could download that small file first, to perform the version checking (without having to download the entire *.exe file)

It'd look something like this:

Dim wc As WebClient = New WebClient
Try 
    ' download a text file with the version stamp
     latestVersion = New Version(wc.DownloadString("http://something/version.txt"))
Catch ex As Exception
     MessageBox.Show("Couldn't download the version file" + ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
     Return
End Try

If (yourVersion < lastestVersion) Then
...
EndIf

If the the EXE file is really big, or you expect that your clients might be on a slow connection, you might also consider using the DownLoadFileAsync method with a progress bar.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.