开发者

How do I interact with prompts using C# System.Diagnostics.Process?

开发者 https://www.devze.com 2022-12-16 18:25 出处:网络
I\'m running a comman开发者_高级运维dline 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,

I'm running a comman开发者_高级运维dline 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?


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消