Self updating application in vb.net

Lucas picture Lucas · Jun 27, 2013 · Viewed 11.1k times · Source

I want my application to self-update if there is a different size executable available on the remote server. The problem I got is that when I kill the process to replace the application executable, nothing more happends - nothing more is executing after the eprocess.Kill() even though I am trying to freeze the thread during the file replacement process. Is there something I am doing wrong?

Here is my code:

            Dim Request As System.Net.WebRequest
            Dim Response As System.Net.WebResponse
            Dim FileSize As Integer
            Request = Net.WebRequest.Create("http://mywebsite.com/File.exe")
            Request.Method = Net.WebRequestMethods.Http.Get
            Response = Request.GetResponse
            FileSize = Response.ContentLength
            Dim mySize As New IO.FileInfo(Application.ExecutablePath)
            If FileSize <> mySize.Length Then                    If File.Exists(tempPath & "\File_tmp.exe") Then
                    File.Delete(tempPath & "\File_tmp.exe")
                End If
                Patcher.DownloadFileAsync(New Uri("http://mywebsite.com/File.exe"), tempPath & "\File_tmp.exe") 'Patcher is defined before, you might think that its not working, but this is just a piece of code, and the new file is downloading properly. The described problem is 
                While Patcher.IsBusy
                    Threading.Thread.Sleep(100)
                End While

                Do While True
                    For Each eprocess As Process In Process.GetProcesses
                        If eprocess.ProcessName = "MyApplication" Then
                            eprocess.Kill()
                        End If
                    Next
                    File.Delete(Application.ExecutablePath)
                    'Copy downloaded file to the application executable path directory
                    File.Copy(tempPath & "\File_tmp.exe", Application.ExecutablePath)
                    Threading.Thread.Sleep(30) 'freeze thread...
                Loop
            End If

Answer

OneFineDay picture OneFineDay · Jun 27, 2013

One way to handle this is make a separate(smaller exe) in your project that handles the downloading of the new version. First it would close the current version and since it's not tied to the original app the uploader app is still running, it downloads, then installs it and then launches the new version.