C#: Process

.NET Array Collections File String Async Cast DataTable DateTime Dictionary Enum Exception For Foreach IEnumerable If IndexOf Lambda LINQ List Parse Path Process Property Regex Replace Sort Split StringBuilder Substring Switch Tuple

Arrow indicates movement

RedirectStandardOutput eliminates the need for output files. It allows us to use a console program directly inside a C# program—without having to rely on output files being written to the disk. We redirect into a StreamReader.




First, this program shows the use of the ProcessStartInfo class and its properties FileName, UseShellExecute and RedirectStandardOutput. It sets up the Process instance for having its output read in the C# program.

Next:The Process.Start method is run and the StandardOutput property on the Process instance is accessed and read from.

Note:This provides a way to redirect the output of a command line executable to the C# program as a string.

C# program that redirects standard output

using System;
using System.Diagnostics;
using System.IO;

class Program
    static void Main()
	// Setup the process with the ProcessStartInfo class.
	ProcessStartInfo start = new ProcessStartInfo();
	start.FileName = @"C:\7za.exe"; // Specify exe name.
	start.UseShellExecute = false;
	start.RedirectStandardOutput = true;
	// Start the process.
	using (Process process = Process.Start(start))
	    // Read in all the text from the process with the StreamReader.
	    using (StreamReader reader = process.StandardOutput)
		string result = reader.ReadToEnd();

    This section shows the output of the process.

7-Zip (A)  4.60 beta  Copyright (c) 1999-2008 Igor Pavlov  2008-08-19

Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...]
Programming tip

ProcessStartInfo is a settings class. It is attached to Process class to change the behavior of the Process invocation. To redirect the output of a process, set UseShellExecute to false and RedirectStandardOutput to true.

Also:You must specify the file name (with the FileName property) before calling Process.Start.


The using-statement is a safe way to use the Process and StreamReader classes. StreamReader is assigned to the StandardOutput stream on the process instance. We can then use ReadToEnd on the StreamReader to access the process text.


Tip:The ReadLine method can also be used on the StreamReader. This may be more efficient for larger amounts of data.


Process: gear

The Process class is available by including the using System.Diagnostics directive. This class interacts with the Windows OS. With it you can invoke any executable on the system. The system must be configured to allow this.



We used ProcessStartInfo to redirect the standard output of a command line executable to the C# program as a string. This provides a way to use a console program directly inside of a C# application. This avoids text files and pipes.