Felix Snow
Felix Snow

Reputation: 3

vb.net Get PID of another application

I'm trying to find the PID of an application that I only know the name of is there some way to get PID from GetProcessByName?

Upvotes: 0

Views: 2958

Answers (2)

Trevor
Trevor

Reputation: 8004

Here is a nice Linq example... Replace PROCESSNAMEHERE with your process name... The variable proc holds the process object and from there you can do anything with it. This object is an Array of any process's it finds...

  Dim proc() As Process = Process.GetProcesses().Select(Function(p) p).Where(Function(n) n.ProcessName = "PROCESSNAMEHERE").ToArray
    If proc IsNot Nothing AndAlso proc.Count > 0 Then
        MessageBox.Show(String.Join(Environment.NewLine, From pr In proc.Select(Function(x) x.Id)))
    End If

Upvotes: 0

Mark
Mark

Reputation: 8160

Process.GetProcessesByName returns an array of Process objects, each of which has an Id property which is the PID.

Dim firefox = Process.GetProcessesByName("firefox")
For Each proc In firefox
    Console.WriteLine("pid={0}", proc.Id)
Next

Since there can be multiple processes returned, you will need to have some way to select the correct one.

Upvotes: 1

Related Questions