Click here to Skip to main content
Click here to Skip to main content

How to redirect Standard Input/Output of an application

, 27 Apr 2007
Rate this:
Please Sign up or sign in to vote.
This articles demonstrates how to redirect Standard Input/Output of an application.


I was working in a project, and I needed to start a secondary process from an application and pass from the application, the needed parameters and inputs to the secondary process and also capture all possible output and errors from the secondary process. I also did not want the exceptions and crashes from the secondary process to be directly displayed to the user. This article briefly expalins how I achieved this. I had to control the standard input, standard output, and the standard error of the secondary process.

Here is an example of my code in action. The shutdown proces is invoked from my application, and it displays the output from the process. As we did not provide an option or parameter for the secondary process, it captures the standard output and displays it.

Screenshot - ProcessStartDemo.png

I made use of the Process class in System.Diagnostics which can start a process for you. (Most of the people know about this, but they don't relaize the extent to which it can help). I will explain the process in a step by step manner with code snippets so that you can understand it easily.

Using the code

Step 1 : Create a ProcessStartInfo object. This is used to execute the executable. ProcessStartInfo has three constructors. You can use any one of them, or you can specify the file name and argument at a later stage (but before starting the process).

ProcessStartInfo processStartInfo = 
  new ProcessStartInfo(executableName, executableParameter);
  • executableName is the full path of the executable. If the executable is placed in a location which is registered in the environment variable, you may omit the full path and specify the executable name.
  • executableParameter is the list of parameters to the executable. The application will be launched with these parameters.

Now, by default, the framework invokes the application using Shell Execute. Set the property UseShellExecute to false.You also need to set the ErrorDialog property to false. So why set the UseShellExecute as false? From the MSDN documentation, you can find that, by default, the shell is used to start the process. Now, to capture the input/output/error, we need to create the process directly from the executable. By doing this, if the application is exposing any input/output/error, we can get in that.

processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;

Step 2 : Now, the most important thing comes; we need to set the redirecting properties to true. Set it according to your convenience. However, you can use any other application.

processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;

Step 3 : Now that things are done, we are ready to capture the input/output/error. Start the process.

Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();

Step 4 : Capture the input /output /error streams for your use. Note that the input stream is a writer. For giving input, you need to write the input string in the input stream.

StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;

Step 5: You are done; use the streams as needed.

Points of interest

The code is simple enough to understand. But these are the points which can help you to understand the Process.

  • WaitForExit() function will wait till the application exits.
  • In the demo application, I have not used the input stream. You can use this as well for giving input to the application.
  • Process.Start() method returns a bool value which indicates that the process has been started successfully.
  • Process.ExitCode returns an error code returned by the process. If it is 0, the process exited without error. This may be helpful for the users in some scenario.
  • EnableRaisingEvents property enables you to raise the Exited event. So if you want to make use of the Exited event, make sure to assign it to true.
  • ProcessStartInfo.ErrorDialog allows you to set if an error message will be displayed or not if the process is not started successfully.
  • You can provide a domain name, username, and password for the process in the ProcessStartInfo object.
  • ProcessStartInfo.CreateNoWindow allows you to create a process without creating a window.


This is the first release of the code. Modifications and feature enhancements will be done on requests. I request users to mail me directly to my email address for any suggestions. In this demo, I have not used the input stream; if you are having trouble with it, then message me and I will provide a sample for that also.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Manish Ranjan Kumar
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

BugThis may have a buffer problem PinmemberFrank Peelo10-Jun-13 6:15 
GeneralRe: This may have a buffer problem PinmemberMember 108720229-Nov-14 17:48 
QuestionCan we run a Java Programme From this Code Pinmembersanjeev46713-Apr-13 6:13 
SuggestionRe: Can we run a Java Programme From this Code PinmemberFrank Peelo10-Jun-13 6:22 
GeneralMy vote of 5 Pinmemberrafapucci20-Feb-13 7:21 
QuestionNeed help on getting output of cdefrag.exe Pinmembervantoora28-Jan-13 3:18 
GeneralGreat article!! Pinmemberpetite_paola13-Jul-12 2:38 
QuestionReading standard input [modified] PinmemberMember 864852515-Feb-12 2:49 
Questionreal time shell , not asynchrone Pinmemberdemotesttest25-Dec-11 5:30 
QuestionCan you help me ! PinmemberAll Time Programming16-Feb-11 6:36 
Hello Manish,
   Thanks for this article. What I want and am loooking for from days is :
I am executing from cmd is openvpn --config client.ovpn --ca certificate.cer --auth-user-pass user.txt
I don't want to pass user.txt file and enter username and password from the application. I have already handled output using handler. At the ed when user wants to exit, I want to input F4 as that's the key to exit openvpn cleanly.
Am not able to figure out how to handle input and output both at the same time. I guess I also need to test in output handler for text like "Enter Esername :" and then pass the username via swriter.
                  processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
                  sb = new StringBuilder();
                  processInfo.UseShellExecute = false;
                  processInfo.RedirectStandardOutput = true;
                  //processInfo.RedirectStandardInput = true;
                  processInfo.CreateNoWindow = true;
                        process = Process.Start(processInfo);
                        sw = process.StandardInput;
                        process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
I get exception at sw = process.StandardInout;
Can you help me with this part. I can't even find any help on internet for this point. I look ahead for your help.

Thanks & Regards,

QuestionHow to simulate the pipe | operator? like "type blabla.txt|abc.exe" Pinmemberatommaki1-Oct-08 22:21 
GeneralNow working when executing application needs an input... Pinmembermasaniparesh8-Apr-08 2:38 
QuestionCan I use this technique to start a process on a server PinmemberSBendBuckeye18-Oct-07 5:42 
GeneralRedirected Standard Output - Problem PinmemberTanushree Paul5-Sep-07 21:29 
Generaluse of redirection opertor PinmemberDurgesh Gupta13-Aug-07 3:43 
GeneralRe: use of redirection opertor Pinmemberdibbs10-Oct-07 3:12 
GeneralRe: use of redirection opertor Pinmembervictorbos12-Jun-12 3:11 
Generalsbs2003 problem PinmemberDeniroSA2-Aug-07 4:24 
QuestionStandard Input in windows forms? Pinmembersansweb18-Jul-07 12:15 
GeneralVery nice - but ... PinmemberP_R_Sousa11-Jun-07 3:55 
GeneralRe: Very nice - but ... Pinmembertw34ky14-Jul-07 6:21 
Questioncan we pass user defined parameters PinmemberPadoor Shiras17-May-07 21:44 
AnswerRe: can we pass user defined parameters PinmemberManish Ranjan Kumar17-May-07 23:08 
GeneralStandard Input simulation Pinmembernyantoine16-May-07 10:43 
AnswerRe: Standard Input simulation PinmemberManish Ranjan Kumar16-May-07 20:49 
QuestionWhat if the exe need some console input interaction ? Pinmemberwisut1-May-07 22:13 
AnswerRe: What if the exe need some console input interaction ? PinmemberMember 108720229-Nov-14 17:40 
QuestionMixed output PinmemberYury Goltsman30-Apr-07 23:07 
AnswerRe: Mixed output PinmemberErik Rydgren1-May-07 21:12 
GeneralRe: Mixed output PinmemberYury Goltsman1-May-07 21:30 
GeneralRe: Mixed output PinmemberErik Rydgren1-May-07 22:08 
GeneralRe: Mixed output PinmemberYury Goltsman2-May-07 1:28 
GeneralRe: Mixed output PinmemberMember 17195938-Feb-08 5:05 
GeneralExcellent Pinmembermerlin98127-Apr-07 10:09 
GeneralGood article; one question about arguments... PinmemberPaul C Smith27-Apr-07 7:03 
AnswerRe: Good article; one question about arguments... PinmemberManish Ranjan Kumar27-Apr-07 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 27 Apr 2007
Article Copyright 2007 by Manish Ranjan Kumar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid