9

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 running. I have found a solution to check if a process is running:

Dim proc As Integer = Process.GetProcessesByName(ProcessName).GetUpperBound(0) + 1 

and return True if >=1 (or just the process number).

My problem is, this is a third-party application, and its process name is not just a name but it contains a version number (which I may not know at run time), and it also seems to add a *32 (so probably a *64 if it is installed in x64 ?).

I need to get a list of running processes, by name, and test if "processname" is a substring of the name. But I haven't been successful in getting a list of names, only process id's.

Thalia
  • 13,637
  • 22
  • 96
  • 190

4 Answers4

8

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).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I have tried this but "Any" doesn't work. Thank you for explaining the *32. – Thalia Jun 15 '12 at 18:41
  • @emptyheaded Just add "Import System.Linq" at the top of your file, and Any should work. It's a LINQ extension method (http://msdn.microsoft.com/en-us/library/bb534972.aspx) – Reed Copsey Jun 15 '12 at 18:53
  • 2
    @Reed Copsey: Should it not be 'p.ProcessName.Contains' instead of p.Name.Contains? And I'm not sure if .Any is caseINsensitive. Otherwise some additional .ToUpper would be helpful to search for the processname? – PeterCo Sep 29 '15 at 11:03
7

You can loop through the running processes like this:

For Each p As Process In Process.GetProcesses()
   Debug.WriteLine(p.ProcessName)
Next
John Koerner
  • 37,428
  • 8
  • 84
  • 134
3

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()
nnm
  • 865
  • 1
  • 8
  • 9
0

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!

EnriqueBet
  • 1,482
  • 2
  • 15
  • 23