0

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.

user3412839
  • 576
  • 3
  • 9
  • possible duplicate of [How do I get the name of the current executable in C#?](http://stackoverflow.com/questions/616584/how-do-i-get-the-name-of-the-current-executable-in-c) – Ed S. Jan 24 '15 at 04:49

1 Answers1

1

Original Filename is a "VerInfo" property which will not be present as a property in the process. It's something that you can set to whatever you want either when you build your application or using something like ResHack. You would need to find the file on disk in order to query that information though.

Consider using something like a named mutex with the process ID that your app creates on startup. e.g. On app start, create a named mutex with "myTestAppNamePid1234", and in your process checking loop try to open mutexes with that name and PID you're looping over.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552