C# Process Redirect Standard Output

by Sam Allen - Updated January 7, 2010

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 classes in the C# language provide a way to redirect the standard output of a process executable to the C# program into a StreamReader. Here we look at how you can redirect the standard output of any executable to a C# string, invoking the command-line executable directly inside the C# program without extra intermediate processing.

Process

Redirecting process output

First, this program text 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.

--- 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 of the program ---
    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...>]

Using ProcessStartInfo class settings. The program text defines the Main entry point and the ProcessStartInfo class is first instantiated. 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-statements and 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.

Process.Start method

In this section, we discuss the Process class and the Process.Start method in general in the C# programming language. 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.

(See Process.Start Command-Line Examples.)

Summary

Here 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, increasing the simplicity of cross-program interactions.

(Do not copy this page.)

Dot Net Perls | Search
Language | DllImport and Dllexport for DLL Interop | Global Variable Use | Process.Start Command-Line Examples | Timer Tutorial | WebClient Tutorial
C# | Integer.TryParse | ArrayList Examples | Bituminous Coal | Sleep Method Use
© 2009 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen