Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Files and I/O

The Process class

Reference for System.Diagnostics.Process in C#: start child programs, redirect standard streams, wait for exit, read exit codes and kill processes.

System.Diagnostics.Process starts and controls operating-system processes. You describe the program with a ProcessStartInfo, call Process.Start, and then wait for it, read what it printed, and check its exit code. The same class also represents programs that are already running, through Process.GetCurrentProcess(), GetProcessById and GetProcesses.

Starting processes is expensive and platform specific, so use it only when you really need another program. Never build a command line by gluing user input into a string.

C#
using System.Diagnostics;

using Process p = Process.Start(new ProcessStartInfo("dotnet", "--version") { RedirectStandardOutput = true })!;

Try it

C# Run a child process and capture its output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# The current process and a failing child

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static Process? Start(ProcessStartInfo startInfo);
public static Process  Start(string fileName, IEnumerable<string> arguments);
public void  WaitForExit();
public bool  WaitForExit(int milliseconds);
public Task  WaitForExitAsync(CancellationToken cancellationToken = default);
public void  Kill();
public void  Kill(bool entireProcessTree);
public int   ExitCode { get; }
public bool  HasExited { get; }
public StreamReader StandardOutput { get; }
public StreamWriter StandardInput { get; }
Name Description
ProcessStartInfo.FileName The program to start.
ProcessStartInfo.ArgumentList Arguments as separate strings, quoted correctly by the runtime. Prefer it to the single Arguments string.
UseShellExecute false to start the program directly (required for redirection); true to let the shell open a file or URL. The default in .NET Core and later is false.
RedirectStandardOutput / Error / Input Capture or feed the standard streams through the Process object.
WorkingDirectory, Environment Working directory and environment variables for the child.
milliseconds How long WaitForExit waits before giving up.
entireProcessTree Also end child processes of the process.

Returns: Start returns the new Process, or null when no new process was started (for example when UseShellExecute reuses an existing one).

Errors: Win32Exception when the file cannot be started; InvalidOperationException when you read ExitCode before the process has exited or redirect a stream you did not request.

Usage notes

  • Dispose it. Process holds an operating-system handle; wrap it in using.
  • Avoid deadlocks. If you redirect both output and error, read them concurrently (as above) before WaitForExit. A child that fills an unread pipe blocks forever.
  • WaitForExit(int) returns false on timeout and leaves the process running. Call Kill(true) yourself if you want it stopped.
  • ExitCode is only valid after exit. By convention 0 means success, but the meaning of other codes belongs to the program.
  • Command-line safety. Use ArgumentList and UseShellExecute = false. With the shell involved, text such as ; rm in user input can run extra commands.
  • Portability. Program names, shell built-ins and path separators differ between Windows and Linux. Test on each platform you ship to, or start Environment.ProcessPath as above.
  • Sandboxes. Hosted environments often forbid starting other programs. Expect Win32Exception or a permission error there.