# Redirecting standard input/output issues

This topic is 4055 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok so I'm using C# .Net to make a utility program to help while using a set of smaller applications. I want to be able to run a console program from within my application. For starters I thought I would try to redirect the input/output of the cmd shell that comes with windows. This is the code that runs when I am just trying to redirect output:
[source lang=C#]
System.Diagnostics.Process myProc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo sInfo  = new System.Diagnostics.ProcessStartInfo("cmd");
sInfo.CreateNoWindow = true;
sInfo.RedirectStandardError = false;
sInfo.RedirectStandardInput = false;
sInfo.RedirectStandardOutput = true;
sInfo.UseShellExecute = false;
sInfo.ErrorDialog = false;
myProc.StartInfo = sInfo;
myProc.Start();


ok. The method "cap_In" simply writes the output to a readonly textbox on the form (although I've noticed I have to put in newlines myself, which I think is part of the problem I'm getting to). When done like this, it works exactly as expected... Output: Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Work Stuff\VS Stuff\PbAnalysisHelper\PbAnalysisHelper\bin\Debug> Yay!, that works... Now I just need to redirect the input so I can write to the console and get it's outputs! So I change "sInfo.RedirectStandardInput = false" to "sInfo.RedirectStandardInput = true". I also make another textbox and anytime I write in it, it sends the text written along with a newline to the input stream. So I start the program again, except now I have the following output to begin with: Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. Where's the last line?? When I type "cd.." into my input textbox my output becomes: Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Work Stuff\VS Stuff\PbAnalysisHelper\PbAnalysisHelper\bin\Debug>cd.. It would appear that cmd is not flushing it's output until it encounters a newline. Is there anyway to force it to read the output? I've tried not using "BeginOutputReadLine()" and instead intermittently checking myProc.StandardOutput.EndOfStream, and using myProc.StandardOutput.ReadToEnd(). Again this works perfectly when the input stream is NOT redirected, but when it is redirected the whole program blocks on the "ReadToEnd()" statement, as if waiting for someone to input something into the console. Any help or thoughts to fix this behaviour will be appreciated, many thanks, ~FenixRoA

##### Share on other sites
Guess no one really knows or else it's not possible through these means.
I'll have to just deal with that.

~FenixRoA

1. 1
2. 2
3. 3
Rutin
24
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002231
×