Advertisement Jump to content
Sign in to follow this  

Redirecting standard input/output issues

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

If you intended to correct an error in the post then please contact us.

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();
            myProc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(cap_In);
            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;

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 this post

Link to post
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.

Thanks for your time guys,

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!