[C#] Processes

A process is a set of resources and the necessary memory allocations used by an application. The “System.Diagnostics” namespace provides types to analyze the processes running on a given machine.

1. System.Diagnostics.Process

The “System.Diagnostics.Process” class represents a process running in a machine.

The key properties are:

  • int Id { get; }  // PID
  • string MachineName { get; } // the name of the computer
  • ProcessModuleCollection Modules { get; } // the modules that have been loaded
  • string ProcessName { get; } // the name of the process
  • bool Responding { get; } // whether the user interface of the process is responding
  • DateTime StartTime { get; }
  • DateTime ExitTime { get; }
  • bool HasExited { get; } // whether the associated process has been terminated
  • int ExitCode { get; }
  • ProcessStartInfo StartInfo { get; set; } // the properties to pass to the Start method of the Process

2. Getting Processes

The “System.Diagnostics.Process” class provides the static methods to get all or specific processes running on a given machine.

public static Process GetCurrentProcess()

public static Process GetProcessById(int processId)
public static Process GetProcessById(int processId, string machineName)

public static Process[] GetProcessesByName(string processName)
public static Process[] GetProcessesByName(string processName, string machineName)

public static Process[] GetProcesses()
public static Process[] GetProcesses(string machineName)

Note that the “GetProcessesByName()” method returns an array of processes, not a single process. (You can open a couple of IE windows.)

foreach (Process p in Process.GetProcesses())
  Console.WriteLine("{0}: {1}", p.Id, p.ProcessName);

foreach(Process ie in Process.GetProcessesByName("iexplore"))
  Console.WriteLine("{0}: {1}", ie.Id, ie.StartTime);

3. Starting / killing Processes – Part 1

The “System.Diagnostics.Process” class has instance methods to manipulate the process lifecycle.

public void Kill() // Immediately stops the associated process.
public bool Start() // Starts (or reuses) the process resource that is specified by the StartInfo property of this Process component
public void Refresh() // Discards any information about the process that has been cached

Killing the currently running process is easy. But starting a new process is a bit more complex.

  • Create a “Process” object
  • Create a “ProcessStartInfo” object
  • Set the “StartInfo” property
  • Call the “Start()” method
string notepad = Environment.SystemDirectory + @"notepad.exe";
Process p = new Process();
ProcessStartInfo start = new ProcessStartInfo(notepad);
p.StartInfo = start;


4. Starting / killing Processes – Part 2

To make your life a little bit easier, the “System.Diagnostics.Process” class provides a couple of static “Start()” methods.

public static Process Start(ProcessStartInfo startInfo)
public static Process Start(string fileName)
public static Process Start(string fileName, string arguments)

Rather than creating a process object manually, you can start a process directly.

string notepad = Environment.SystemDirectory + @"notepad.exe";

Process p = Process.Start(notepad);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s