C# RedirectStandardOutput Example

Arrow indicates movement

You have a console application written in any language and want to invoke the application directly inside a C# program, without having to rely on output files being written to the disk. The Process and ProcessStartInfo types, with the RedirectStandardOutput property, provide a way to redirect the standard output of a process executable to the C# program into a StreamReader.

Example

First, this program demonstrates the use of the ProcessStartInfo class and its properties FileName, UseShellExecute, and RedirectStandardOutput to set up the Process instance for having its output read to the C# program. Next, the Process.Start method is invoked and the StandardOutput property on the Process instance is accessed and read from, providing a way to redirect the output of a command-line executable to the C# program as a string.

This C# example program uses the RedirectStandardOutput property. It requires System.Diagnostics.

Program that redirects standard output [C#]

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();
		Console.Write(result);
	    }
	}
    }
}

Output
    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>...]
       [<@listfiles...>]

ProcessStartInfo class settings. The ProcessStartInfo class is a settings class that can be attached to the Process class or Process.Start method to mutate the behavior of the Process invocation. To redirect the output of a process such as the command-line executable used here, you should set the UseShellExecute property to false and the RedirectStandardOutput property to true. You must also specify the file name before calling Process.Start.

Using keyword

Resource acquisition statements. The using-statement provides a safe and efficient way to use the Process and StreamReader class. The StreamReader class is assigned to the StandardOutput stream on the process instance. We can then use ReadToEnd on the StreamReader to access the process text. The ReadLine method can also be used on the StreamReader.

Using Statement Calls Dispose Using StreamReader ReadLine Reads File Into List

Process.Start

Process illustration

The Process class is always available by including the using System.Diagnostics directive, or by using the fully qualified name System.Diagnostics.Process. This class interacts with the Windows operating system and with it you can invoke any executable on the system, provided the system is configured to allow this.

Process.Start Examples

Summary

The C# programming language

We looked at the ProcessStartInfo class and the Process class when used 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 application, which may be written in an entirely different language and environment, directly inside of a C# application. This avoids the requirement of needing to create text files and pipes to transfer data between applications.

.NET Framework Info
.NET