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