我正在尝试使用StandardInput和StandardOutput来封装控制台应用程序。但当控制台应用程序需要输入密码等内容时,我会遇到困难。
我想从StandardOutput中读取信息,提示用户输入,并将用户的输入写回控制台应用程序的StandardInput中。看起来很简单,以下是我目前的代码:
问题在于
我想从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()
只是在等待换行符。 在这种情况下,它只会一直等待,因为没有要读取的行...控制台应用程序已经输出了没有换行符的文本并正在等待输入。巧合的是,当我手动终止进程时,事件会触发并且我会收到文本。
有没有办法判断进程是否正在等待标准输入?或者我是否错过了一个完全明显的方法来实现目标?