1

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
Lucas
  • 3,517
  • 13
  • 46
  • 75

5 Answers5

1

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.

OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • You mean something like downloading the `Updater.exe` to the temporary path, then let it run and kill the main app? Seems legit, I will try that. – Lucas Jun 27 '13 at 04:41
  • Btw. what do you think about doing this job in batch ? It wouldnt require additional applications... – Lucas Jun 27 '13 at 04:49
0

There is built in functionality to do this for you.

Have a look at ClickOnce. This negates the need to have a separate application to do the updating for you and allows you to specify a minimum and recommended version, whether the app checks for a new version before or after it starts, etc.

Some more links that may be of use:

http://msdn.microsoft.com/en-us/magazine/cc163973.aspx http://www.codeproject.com/Articles/38546/Click-Once-Deployment-Technique

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
0

I say you write a function that creates a batch file which

  • downloads the new version of your exe
  • closes your app and overwrites your exe with the new.
  • Opens your app

Then your app on startup should look for a such batch file and erase it.

Mandolf0
  • 11
  • 1
0

Make an updater whose work will be downloading the files from the remote server then copies/moves them into your main application directory. I've been using this approach for a while now and it works fine. Maybe this approach here will work for you https://www.codeproject.com/Articles/35787/VB-NET-Background-File-Downloader

Albert Alberto
  • 801
  • 8
  • 15
-1

Thought i would add my suggestion to the mix being that this is still the top result in google for stackoverflow.

Firstly.. I can not stand the ClickOnce Solution as it modifies to much of the project and requires other steps and so on.. So i ruled that out after 10 minutes of looking into it.

Also as for having another exe to handle the updates..This was not ideal for me either... So i had my main exe/setup install do all this for me.

Basically i have a website where i host the setup install files with a simple JSON data file to hold the version information. This is accessed via the application itself using a http request and json decoding to query the latest version and compare it against the apps current version.

If a new version is found, the application will download a copy of the new install and place it inside of the applications main directory in a temp folder/installers folder.

Once the download has completed, i execute the installer from within the application and dont waitforexit. The next like of code will trigger the applications save & exit functions.

At this point you have the install running but the main application has exited.. From here the installer can continue as normal and start the application on complete.

You can of course vary alot of things here like silent installs and so on.. but using this method, it allows me to get away with just the normal installer and app exe..

Angry 84
  • 2,935
  • 1
  • 25
  • 24