Skip to Content
C# 3.0 Cookbook, 3rd Edition
book

C# 3.0 Cookbook, 3rd Edition

by Jay Hilyard, Stephen Teilhet
December 2007
Intermediate to advanced
896 pages
19h 57m
English
O'Reilly Media, Inc.
Content preview from C# 3.0 Cookbook, 3rd Edition

8.8. Capturing Standard Output for a Process

Problem

You need to be able to capture standard output for a process you are launching.

Solution

Use the RedirectStandardOutput property of the Process.StartInfo class to capture the output from the process. By redirecting the standard output stream of the process, youread it when the process terminates. UseShellExecute is a property on the ProcessInfo class that tells the runtime whether or not to use the Windows shell to start the process or not. By default, it is turned on (true) and the shell runs the program, which means that the output cannot be redirected. This needs to be set to off and then the redirection can occur. The UseShellExecute property is set to false to ensure this is not started using the Windows shell for your purposes here.

In this example, a Process object for cmd.exe is set up with arguments to perform a directory listing, and then the output is redirected. A log file is created to hold the resulting output, and the Process.Start method is called:

	    // See 12.21 for more info on redirection
	    Process application = new Process();
	    // Run the command shell.
	    application.StartInfo.FileName = @"cmd.exe";

	    // Get a directory listing from the current directory. 
	    application.StartInfo.Arguments = @"/Cdir " + Environment.CurrentDirectory; 
	    Console.WriteLine("Running cmd.exe with arguments: {0}",
	                    application.StartInfo.Arguments);

	    // Redirect standard output so we can read it.
	    application.StartInfo.RedirectStandardOutput = true; application.StartInfo.UseShellExecute ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C# 6.0 Cookbook, 4th Edition

C# 6.0 Cookbook, 4th Edition

Stephen Teilhet, Jay Hilyard
C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet

Publisher Resources

ISBN: 9780596516109Errata Page