如何使用 C# System.Diagnostics.Process 与提示进行交互?
我正在通过 C# System.Diagnostics.Process 运行命令行进程。
当我在 cmd.exe 中输入相同的命令时,它立即提示我输入密码。输入密码后,该过程将继续完成。
但是,使用 System.Diagnotic.Process,我重定向的标准输出都不会生成密码提示。如果是这样,我将使用重定向的标准输入的 WriteLine 方法以编程方式输入它。
使用 Process 对象通常不会在输出中看到提示吗?提示符与其他标准输出行不同吗?如果是这样,如何将“提示事件”引入我的应用程序,输入输入,然后让该过程像在 cmd.exe 中一样继续进行?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您重定向用 C/C++ 编写的程序的输出时,stdout 流会切换到缓冲模式。提示就在那里,它只是从未从内部缓冲区刷新到流,因为缓冲区没有填满。只需写下密码就可能解决您的问题。
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.
为
Process.Start
调用准备ProcessStartInfo
时,请务必正确设置以下属性:RedirectStandardInput
RedirectStandardOutput
RedirectStandardError
创建
Process
对象后,请确保对适当的重定向流使用以下属性:StandardInput
StandardOutput
标准错误
When preparing the
ProcessStartInfo
for yourProcess.Start
call, be sure to properly set the following properties:RedirectStandardInput
RedirectStandardOutput
RedirectStandardError
And after creating your
Process
object make sure to use the following properties for the appropriate redirected streams:StandardInput
StandardOutput
StandardError