I have a project called Test
which is a Form with 1 button. I want to make it so that when I click on the button it kills all the older instances of it.
So here's what I did:
Private Sub removeOldInstances()
'gets all processes with the same name as the project.
Dim oldProcess() As Process = Process.GetProcessesByName("Test")
Dim currentProcessId As Integer = Process.GetCurrentProcess.Id
For Each p As Process In oldProcess
If p.Id <> currentProcessId Then
p.Kill()
End If
Next
End Sub
And I made the button call that on click.
So I tested it and it works fine. But the problem occurs when I renamed the .exe to something else and tried to run it. So lets say I renamed it to Test1
instead of Test
. If I run 2 instances of it and from one of them if I click the button it won't kill the older instance of it. This is probably because in my removeOldInstances
method, it gets processes by the name "Test"
.
I noticed that if you look at the properties of the program, under the details tab is has something called original filename
. I was thinking maybe I can get all the processes using that, but how would I do that?
If anyone could give a hint or another way of doing this that would be really appreciated.