I am trying to find out if an instance of an application (not vb.net) is already running - because I will want to start it but I don\'t want to start it if it is already run
I need to get a list of running processes, by name, and test if "processname" is a substring of the name.
You could use:
Dim procExists as Boolean = Process.GetProcesses().Any(Function(p) p.Name.Contains(processName))
This will look through all of the processes, and set the procExists
value to True if any process which contains processName
exists in the currently executing processes. This should handle the existence of the unknown version number as well as the *32
that may occur if you're running on a 64bit OS (that's the WOW64 flag saying that it's a 32bit process running on a 64bit OS).
another way:
Dim psList() As Process
Try
psList = Process.GetProcesses()
For Each p As Process In psList
Console.WriteLine(p.Id.ToString() + " " + p.ProcessName)
Next p
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadKey()
You can loop through the running processes like this:
For Each p As Process In Process.GetProcesses()
Debug.WriteLine(p.ProcessName)
Next
Just tried the answer posted of Reed Copesy and this seems to be changed, what worked for me is:
Dim procExists as Boolean = Process.GetProcesses().Any(Function(p) p.ProcessName.Contains(processName))
Also is possible to retrieve the array by name and check directly if is contained on the process array:
Process.GetProcessesByName(processName).Length > 0
Thanks!