控制台应用程序提示输入
我正在尝试使用 StandardInput 和 StandardOutput 将控制台应用程序包装在一起。我被困在控制台应用程序提示输入密码等地方。
我想从 StandardOutput 读取,使用读取的文本提示用户,并使用其 StandardInput 将用户的输入写回控制台应用程序。看起来很简单,这就是我目前所拥有的:
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();
问题是 BeginOutputReadLine()
正在这样做......等待行结束。在这种情况下,它只是坐、坐、坐,因为没有行可以读取...控制台应用程序已写出没有行结尾的文本,正在等待输入。 巧合的是,当我手动终止进程时,事件会触发并收到文本。
有没有办法告诉进程正在等待 StandardInput?或者我错过了一个完全明显的方法来实现目标?
I'm trying to put together a wrapper around a console application using StandardInput and StandardOutput. I'm getting stuck where the console application would prompt for input such as a password.
I'd like to read from StandardOutput, prompt the user using the read text, and write the user's input back to the console application using its StandardInput. Seems easy enough, here's what I have currently:
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()
is doing just that...waiting for a line ending. In this case it just sits, and sits, and sits because there is no line to read...the console application has written out text with no line ending and is waiting for input. Coincidentally, when I manually kill the process the event fires and I get the text.
Is there a way to tell that the process is waiting for StandardInput? Or am I missing a completely obvious way to accomplish the goal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非您需要异步的东西,否则您可能需要 ReadToEnd。
以下是所有StreamReader 方法的列表
Unless you need something asynchronous you probably want ReadToEnd.
Here is a list of all StreamReader Methods
process.StandardOutput.BaseStream.BeginRead(...) 是您的 readline 的潜在替代品,并且不会等待行结束,但是您需要知道什么终止输出,以确定何时不开始等待下一个数据块
process.StandardOutput.BaseStream.BeginRead(...) is a potential substitute for your readline, and that will not wait for a line ending however you'd need to know what terminates the output to figure out when not to start wait for the next chunk of data
正如 Rune 所说,您可以直接访问进程的输出流 (process.StandardOutput) 并从那里读取(如果您不想等到在控制台应用程序中输入换行符),但这意味着您需要定期检查新数据。
要与应用程序交互,您只需写入进程的 StandardInput(创建写入 process.StandardInput 的 StreamWriter)。
MSDN 文档中有一个很好的写入示例(http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx)。
希望这有帮助
As Rune said, you can access directly to the output stream of the process (process.StandardOutput) and read from there (if you don't want to wait until a line break is entered into the console app), but this means that you need to check periodically for new data.
To interact with the application, you can just write to the StandardInput of the process (create a StreamWriter that writes to the process.StandardInput).
A nice sample of writing to it is on the MSDN documentation (http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx).
Hope this helps
您需要使用同步读取方法并自行处理任何必要的线程。下面的代码不会告诉您需要输入,但您将能够检测到显示的提示。
You need to use the synchronous read method and handle any necessary threading yourself. The below code won't tell you that input is expected, but you will be able to detect that a prompt is displayed.