Request a console application for input

I am trying to build a wrapper around a console application using StandardInput and StandardOutput. I get stuck when a console application asks for input, such as a password.

I would like to read from StandardOutput, ask the user to use the read text, and write the user's input back to the console application using standard input. It seems simple enough, here is what I have at the moment:

Process process = new Process() { StartInfo = { FileName = "bin\\vpnc.exe", Arguments = "--no-detach --debug 0", CreateNoWindow = true, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, } }; process.OutputDataReceived += (s, args) => { textBlock1.Dispatcher.Invoke(new Action(() => { textBlock1.Text += args.Data; })); }; process.Start(); process.BeginOutputReadLine(); 

The problem is that BeginOutputReadLine() does just that ... waiting for the end of the line. In this case, it just sits, sits and sits, because there is no line to read ... the console application wrote out the text without ending the line and waits for input. By the way, when I manually kill the process, the event fires, and I get the text.

Is there any way to say that the process is expecting StandardInput? Or am I missing a very obvious way to achieve my goal?

+4
source share
4 answers

process.StandardOutput.BaseStream.BeginRead (...) is a potential replacement for your readline, and it will not wait for the end of the line, but you need to know what the output ends in order to find out when not to start, wait for the next piece of data

+1
source

If you don't need something asynchronous, you probably want ReadToEnd .

Here is a list of all StreamReader Methods

+1
source

As Rune says, you can directly access the process output (process.StandardOutput) and read from there (if you do not want to wait until a line break is entered in the console application), but this means that you need to periodically check for new data.

To interact with the application, you can simply write the StandardInput of the process (create a StreamWriter that writes to process.StandardInput).

A good sample for writing on it is contained in the MSDN documentation ( http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx ).

Hope this helps

0
source

You need to use the synchronous method of reading and independently process any necessary streams. The code below does not indicate that an entry is expected, but you may find that an invitation is displayed.

  char[] b = new char[1024]; while (!process.HasExited) { int c = process.StandardOutput.Read(b, 0, b.Length); context.Response.Write(b, 0, c); Thread.Sleep(100); } 
0
source

All Articles