TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# RedirectStandardOutput Example

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

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.

StreamReader

Example. 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();
		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 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.

UsingReadLine

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

Process.Start. 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.

Process

Summary. 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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf