Search code examples
c#.netprocess

How can I know if a process is running?


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


Solution

  • 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);
    }