tags:

views:

366

answers:

2

I'm running a commandline process via C# System.Diagnostics.Process.

When I enter this same command in cmd.exe, it immediately prompts me for a password. After I type in the password, the process continues to completion.

However, using System.Diagnotic.Process, none of the standard output I'm redirecting ever produces a password prompt. If it did, I would programmatically enter it using the redirected standard input's WriteLine method.

Is it usual to not see prompts in the output using the Process object? Are prompts different from other standard output lines? If so, how can bring "prompt-events" into my application, enter input, and then have the process proceed the same way it does in cmd.exe?

+1  A: 

When you redirect the output of a program written in C/C++, the stdout stream switches to buffered mode. The prompt is there, it just never got flushed from the internal buffer to the stream because the buffer didn't get filled to capacity. Just writing the password is likely to solve your problem.

Hans Passant
Most command-line programs will prompt for a password on the stderr stream, no?
Daniel Pryden
I've tried using Process.StandardInput.WriteLine("password"), but the process just hung as the I haven't entered the password.The first person mentioned that I had to make redirect streams. If that is so, why does calling this method not cause an exception. It acts like it already has an input stream as a property of the process object.
LonnieBest
Daniel, I'm adding both error out and regular output to the textbox, when their corresponding events happen. Yet, I don't ever get to see the prompts I see when executing the exact same command in cmd.exe
LonnieBest
I'm definitely get both standard output and errors, but prompts are never shown. Have any of you actually interacted with prompts using the System.Diagnostics.Process object?
LonnieBest
Yah, cmd.exe, doesn't work either. But no, I probably didn't try your program, whatever it might be.
Hans Passant
+1  A: 

When preparing the ProcessStartInfo for your Process.Start call, be sure to properly set the following properties:

  1. RedirectStandardInput
  2. RedirectStandardOutput
  3. RedirectStandardError

And after creating your Process object make sure to use the following properties for the appropriate redirected streams:

  1. StandardInput
  2. StandardOutput
  3. StandardError
Aviad P.
You can't set these properties as they are read-only... however, you can read from / write to the streams returned by these properties
Thomas Levesque
RedirectStandardInput, RedirectStandardOutput, and RedirectStandardError of the StartInfo object have all been set to true, as you are trying to confirm.
LonnieBest
However, I've been successfully obtaining output and errors from the the OutputDataReceived and ErrorDataReceived events via two seperate DataRecievedEventHandler objects that call a custom method that outputs them to a textbox (without creating any streams explicitly).
LonnieBest
I have not created any redirect streams, because it seems they are built-in. I've confirmed that the events receive and pass data to my custom method each time an error or output occurs. Could these, however, not output "prompts outputs" for some reason?As for writing input, why is it necessary to create a redirect stream. Doesn't, Process.StandardInput.WriteLine("password") suffice in inputing when RedirectStandardInput equals true?
LonnieBest
I'm having a hard time understand the process object conceptually, if it requires you to create these streams, because many of the methods and events imply that such things are completely abstracted.
LonnieBest
I'm sorry, Thomas Levensque is right, I was going from memory and was wrong. When you specify `true` in the `RedirectStandard...` properties, the streams are created for you automatically when the `Process` is started.
Aviad P.