C# Tips

C# Tip Article

How to get process handle from process name in C#

Question

A question from facebook group: How can we get process handle or process id from running process name in C#?

Tip

Answer

System.Diagnostics.Process has a method called GetProcessesByName() to get all processes having the process name. The parameter in the method (which is process name) should be file name only without .EXE extention. The method can return multiple processes since there might be multple processes with the same process name.

Id and Handle properties in the Process class mean native PID and native handle value.

// To get NOTEPAD.EXE processes
var processes = Process.GetProcessesByName("notepad");
foreach (var process in processes)
{
	Console.WriteLine("PID={0}", process.Id);
	Console.WriteLine("Process Handle={0}", process.Handle);                
}