Imports System.Net Imports System.IO Public Class Form1 Private Sub updateappclosing() 'called from form_closing event Dim st As String = Application.ExecutablePath Dim appname As String = Mid(st, st.LastIndexOf("\") + 2, st.Length - 5 - st.LastIndexOf("\")) 'get executable name without .exe.Don't use substring here.. If Not appname = "Self updater Update" Then 'normal program is running still Call dolb1("Just checking for updates before closing, this could take 10 to 20 secs", Color.Crimson) Me.Refresh() Call updatecheck() 'this will download new version if available Dim app_path As String = Application.StartupPath If File.Exists(Application.StartupPath & "\Self updater Update.exe") Then 'check if download exists in folder Dim proc As New System.Diagnostics.Process() proc = Process.Start(Application.StartupPath & "\Self updater Update.exe") 'start downloaded progam proc.WaitForExit(0) End If 'old original program ends here End If End Sub Sub updatecheck() Dim filepathstr, versionstr, filename As String versionstr = "http://olympia-art.com/Self updater/version.ver" filepathstr = "http://olympia-art.com/Self updater/Self updater Update.exe" filename = "Self updater Update.exe" Threading.Thread.Sleep(400) 'delay to show download message If My.Computer.Network.Ping("www.olympia-art.com") Then Call dolb1("Connected, getting version number", Color.Green) Me.Refresh() ' Threading.Thread.Sleep(400) Dim client As New Net.WebClient Dim newVersion As String = "" Try newVersion = client.DownloadString(versionstr) 'read version file Catch ex As Exception client.Dispose() Call dolb1("Can't find file 'version.ver'", Color.Crimson) Me.Refresh() Threading.Thread.Sleep(2000) Exit Sub End Try If newVersion > Application.ProductVersion Then Call dolb1("Downloading update", Color.Green) Threading.Thread.Sleep(200) Me.Refresh() Try client.DownloadFile(filepathstr, Application.StartupPath & "\" & filename) 'try download Catch ex As Exception client.Dispose() Call dolb1("Can't find " & filename, Color.Crimson) Me.Refresh() Threading.Thread.Sleep(2000) Exit Sub End Try client.Dispose() Call dolb1("Update finished", Color.Green) Me.Refresh() Threading.Thread.Sleep(400) Else Call dolb1("No update available", Color.Blue) Me.Refresh() Threading.Thread.Sleep(700) My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\CNC Engrave\UD", "UpdateTime", Now) 'reset 7 day timer End If Else Call dolb1("No internet connection found", Color.Crimson) Me.Refresh() Threading.Thread.Sleep(500) End If End Sub Private Sub updateappstarting() 'called from form_load event Dim st As String = Application.ExecutablePath Dim appname As String = Mid(st, st.LastIndexOf("\") + 2, st.Length - 5 - st.LastIndexOf("\")) 'st.substring doesn't work... If appname = "Self updater Update" Then 'downloaded and started by old prog at shutdown If File.Exists("Self updater.ex") Then 'delete any previous backups of original Try File.Delete("Self updater.ex") Catch ex As Exception End Try End If If File.Exists("Self updater.exe") Then 'create a backup of original as .ex File.Copy("Self updater.exe", "Self updater.ex") Try File.Delete("Self updater.exe") 'delete original old program Catch ex As Exception End Try Try File.Copy("Self updater Update.exe", "Self updater.exe") 'replace old with new, renamed to old Catch ex As Exception End Try My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Self updater\UD", "UpdateTime", Now) 'reset 7 day timer Application.Exit() 'update finished End If ElseIf File.Exists("Self updater Update.exe") Then 'normal startup of program, delete update file if there File.Delete("Self updater Update.exe") End If End Sub Private Sub updatetime() 'download timer If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Self updater\UD", "UpdateTime", Nothing) Is Nothing Then My.Computer.Registry.CurrentUser.CreateSubKey("Software\Self updater\UD") 'first run, create key My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Self updater\UD", "UpdateTime", Now) Exit Sub End If Dim was As DateTime Dim elapsed_time As TimeSpan was = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Self updater\UD", "UpdateTime", Nothing) 'get old time elapsed_time = Now.Subtract(was) 'subtract from now If elapsed_time.TotalDays > 0 Then 'just 0 for demonstration, otherwise 7 days or whatever you want Call updateappclosing() 'check for update End If End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 'Call ud() Call updatetime() 'read registry whether 7 days have elapsed and that calls updateappclosing End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Call updateappstarting() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Application.Exit() End Sub Sub dolb1(ByVal str As String, ByVal col As Color) TextBox1.Text = str TextBox1.ForeColor = col End Sub 'Public Sub updatecheck() 'here is an alternative version with byte counter display ' If IsConnectionAvailable() = True Then ' Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://olympia-art.com/Self updater/version.ver") ' Dim response As System.Net.HttpWebResponse = request.GetResponse() ' Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream()) ' Dim newestversion As String = sr.ReadToEnd() ' Dim currentversion As String = Application.ProductVersion ' If newestversion.Contains(currentversion) Then ' My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Self updater\UD", "UpdateTime", Now) 'reset 7 day timer ' Call dolb1("No update available", Color.Blue) ' Me.Refresh() ' Threading.Thread.Sleep(700) ' Else ' Dim BUFFERLENGTH As Integer = 2500 ' Dim buffer(BUFFERLENGTH) As Byte ' Dim size As Integer = 0 ' Dim bytesRead As Integer = 0 ' Dim url As String = "http://olympia-art.com/Self updater/Self updater Update.exe" ' a copy of the updated program with update in the name ' Dim req As HttpWebRequest = WebRequest.Create(url) ' Dim res As HttpWebResponse = req.GetResponse() ' Dim contents As Stream = res.GetResponseStream() ' Dim app_path As String = Application.StartupPath ' Dim fileApndStrm As New FileStream(app_path & "\Self updater Update.exe", FileMode.OpenOrCreate, FileAccess.Write) ' While size <> -1 ' size = contents.Read(buffer, 0, BUFFERLENGTH) ' If size = 0 Then Exit While ' bytesRead += size ' Call dolb1(bytesRead & " bytes / " & FormatNumber((bytesRead / 1024), 2) & " kb", Color.Blue) ' Me.Refresh() ' If size < BUFFERLENGTH Then ' Dim buff(size) As Byte ' Array.Copy(buffer, buff, size) ' fileApndStrm.Write(buff, 0, size) ' buff = Nothing ' Else ' fileApndStrm.Write(buffer, 0, size) ' End If ' Array.Clear(buffer, 0, size) ' End While ' fileApndStrm.Close() ' contents.Close() ' buffer = Nothing ' End If ' Else ' Call dolb1("No internet connection found", Color.Crimson) ' Me.Refresh() ' Threading.Thread.Sleep(500) ' End If 'End Sub 'Public Function IsConnectionAvailable() As Boolean ' Dim objUrl As New System.Uri("http://olympia-art.com/Self updater/version.ver") 'check connection to internet ' Dim objWebReq As System.Net.WebRequest 'version.ver contains 1.0.0.1. If it was called .txt, then the cached file would be sent instead... ' objWebReq = System.Net.WebRequest.Create(objUrl) ' Dim objresp As System.Net.WebResponse ' Try ' objresp = objWebReq.GetResponse ' objresp.Close() ' objresp = Nothing ' Call dolb1("Connected", Color.Green) ' Me.Refresh() ' Threading.Thread.Sleep(300) ' Return True ' Catch ex As Exception ' objresp = Nothing ' objWebReq = Nothing ' Return False ' End Try 'End Function End Class