Reputation: 69
I need the process ID for a program that is already running on the computer. How would I go about doing this? (The process isn't started from Process.Start())
Upvotes: 1
Views: 358
Reputation: 52290
Use GetProcessesByName or just GetProcesses with a bit of LINQ, depending on how you intend to identify the program.
using System;
using System.Diagnostics;
using System.ComponentModel;
void Example()
{
// Get all processes running on the local computer.
var localProcesses = Process.GetProcesses();
//Get all processes with a name that contain "Foo" in the title
var fooProcess = localProcesses.Where(p => p.MainWindowTitle.Contains("Foo"));
// Get all instances of Notepad running on the local computer.
var notepad = Process.GetProcessesByName("notepad").Single();
}
Once you have the Process object, you can get its ID with the Id property.
var id = process.Id;
Upvotes: 2