How can I know if a process is running?

reshefm picture reshefm · Nov 4, 2008 · Viewed 235.7k times · Source

When I get a reference to a System.Diagnostics.Process, how can I know if a process is currently running?

Answer

Patrick Desjardins picture Patrick Desjardins · Nov 4, 2008

This is a way to do it with the name:

Process[] pname = Process.GetProcessesByName("notepad");
if (pname.Length == 0)
  MessageBox.Show("nothing");
else
  MessageBox.Show("run");

You can loop all process to get the ID for later manipulation:

Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist){
   Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}