Process.start how to get the output?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To get output from a process in .NET, you must start it with redirected standard streams. The important settings are UseShellExecute = false and RedirectStandardOutput = true, and often RedirectStandardError = true as well.
The subtle part is avoiding deadlocks. Reading output incorrectly can block if the child process writes enough data to fill one stream while your code is waiting on the other.
The Minimum Working Setup
Here is a clean starting point:
That gives you the child process output as strings you can log, parse, or display.
Why UseShellExecute Must Be false
This setting is required for stream redirection:
If it stays true, the operating system shell owns the process launch path and .NET cannot redirect the standard streams the way you want.
This is one of the most common reasons "I set RedirectStandardOutput = true but got nothing" happens.
Capture Standard Error Too
If the process can write diagnostics or errors, redirect standard error as well:
That matters because many command-line tools put useful information on stderr even when they succeed partly or fully. If you ignore stderr, you can miss the only clue about what went wrong.
Avoid Deadlocks by Reading Streams Properly
A naive pattern is:
This sometimes works, but it can deadlock if the child process fills one buffer while your code is blocked reading the other stream at the wrong time.
Asynchronous reading is safer for real tools that may write a lot of output:
That pattern is much more robust.
Line-by-Line Processing
If you want output as it arrives rather than at the end, use event handlers:
This is useful for live logs, progress reporting, or long-running commands.
Exit Codes Matter Too
Output alone is not enough. Check the exit code:
A process may print something helpful and still fail, or print nothing and succeed. Treat output and exit status as separate signals.
Common Pitfalls
The biggest pitfall is forgetting UseShellExecute = false. Without it, redirection does not work.
Another common problem is reading stdout and stderr in a blocking way that can deadlock when the child process writes a lot of data.
People also forget that stderr is not the same as failure and stdout is not the same as success. You need the exit code too.
Finally, do not use Process.Start("some command with spaces") as a single raw string and hope the platform parses it the way you intended. Prefer FileName plus Arguments.
Summary
- Redirect output with
RedirectStandardOutput = trueandUseShellExecute = false. - Redirect stderr as well if you care about diagnostics.
- Prefer asynchronous or event-driven reading to avoid deadlocks.
- Check the process exit code in addition to the output text.
- Use structured
ProcessStartInfoinstead of relying on shell parsing.
Related reading
- Process.WaitForExit asynchronously
- Produce a random number in a range using C
- Programmatically detecting Release/Debug mode .NET
- programmatically get BPM of a wave or MP3 from .Net
- Programming a distributed application written in C#, Ruby and Java using XML-RPC
- Progress info using HttpClient
- Prompt Dialog in Windows Forms
- Proper naming convention for a .NET Delegate type?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.