当在C#中读取控制台命令输出时,"StandardOut has not been redirected or the process hasn't started yet"。

15 浏览
0 Comments

当在C#中读取控制台命令输出时,"StandardOut has not been redirected or the process hasn't started yet"。

感谢@user2526830提供的代码。基于该代码,我在我的程序中添加了几行,因为我想读取SSH命令的输出。以下是我的代码,该代码在while行处出现错误:

StandardOut未重定向或进程尚未启动。

我想实现的目标是将ls的输出读入字符串中。

ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = @"f:\plink.exe";
startinfo.Arguments = "-ssh [email protected] -pw abc123";
Process process = new Process();
process.StartInfo = startinfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.StandardInput.WriteLine("ls -ltr /opt/*.tmp");
process.StandardInput.WriteLine("exit");
process.StartInfo.RedirectStandardOutput = true;
while (!process.StandardOutput.EndOfStream)
{
    string line = process.StandardOutput.ReadLine();
}
process.WaitForExit();
Console.ReadKey();

0