0

I have a problem here in my code.I want to delete the existing application exe in my downloadPath and copy the new version of the application that is located in GetFilesFrom then open the application again. But after the proc.Kill() execute, the next line of code is not continue. Do I have a missing code?

Thank you.

    Public CurExeVersion As String = Application.ProductVersion '1.0.1.0 version in my desktop app
    Public version = FileVersionInfo.GetVersionInfo("\\Bdpc\inhouse apps\Admin Management\Application Files\Admin Management_1_0_0_0\Admin Management.exe")
    Public ServerVersion As String = version.FileVersion '1.0.2.0 version in server
    Dim client As New Net.WebClient
    If CurExeVersion <> ServerVersion Then

        If MessageBox.Show("Update is ready to install", "Do you want to continue?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim processName As String = "Admin Management"
            Dim GetFilesFrom As String = "\\Bdpc\inhouse apps\Admin Management\Application Files\Admin Management_1_0_0_0\Admin Management.exe"
            Dim downloadPath As String = "D:\Admin Management_1_0_0_0\Admin Management.exe"

            If Process.GetProcessesByName(processName).Length > 0 Then
                
                For Each proc As Process In Process.GetProcessesByName(processName)
                    proc.Kill()
                Next

                
                While Process.GetProcessesByName(processName).Length > 0
                    Threading.Thread.Sleep(100)
                End While

            End If

            IO.File.Delete(downloadPath)
            IO.File.Copy(GetFilesFrom, downloadPath)
            Process.Start(downloadPath)

        End If
    End If
Raffy
  • 9
  • 3
  • "not working" is a little bit vague – Magnus May 02 '23 at 09:13
  • I can see a possible problem with the version number comparison: if the one on the server is *older* than the current version, it will replace it. To compare versions, you should use the [Version.CompareTo Method](https://learn.microsoft.com/en-us/dotnet/api/system.version.compareto). – Andrew Morton May 02 '23 at 11:12
  • Could you use a separate updater program? A program killing itself won't be running the next line. – Andrew Morton May 02 '23 at 11:13
  • I just want to replace my exe file in my computer if the version in server is higher. Do I need to use updater? – Raffy May 03 '23 at 00:11
  • @Raffy Yes, you need a separate program to replace the file: you cannot update a program from within itself (unless you want to reboot Windows to do it, which is not very elegant and people will be annoyed at you). – Andrew Morton May 03 '23 at 09:55

1 Answers1

0

It sounds like the first process to kill is the one you're running. Maybe change the line:

proc.Kill()

to

If proc.ID <> Environment.ProcessID Then
    proc.Kill()
End If

I haven't tested this code, but it ought to work.

Please reference this, too, where I got the current process: How to get the current ProcessID?

Michael Foster
  • 420
  • 6
  • 12
  • ```Dim currentProcessId As Integer = Process.GetCurrentProcess().Id For Each proc As Process In Process.GetProcessesByName(processName) If proc.Id <> currentProcessId Then proc.Kill() End If Next``` I added the `currentProcessId` instead of `Environment.ProcessID` I'm having a error if I use that. But nothing happens – Raffy May 03 '23 at 00:48
  • From the link: "The upcoming .NET 5 introduces Environment.ProcessId which should be preferred over Process.GetCurrentProcess().Id as it avoids allocations and the need to dispose the Process object." – Michael Foster May 03 '23 at 12:02