本文介绍了“StandardOut 尚未重定向或进程尚未启动"在 C# 中读取控制台命令输出时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
感谢@user2526830 提供代码.基于该代码,我在程序中添加了几行,因为我想读取 SSH 命令的输出.下面是我的代码,它在 while
Thanks to @user2526830 for the code. Based on that code I added few lines to my program since I want to read the output of the SSH command. Below is my code which gives an error at line while
StandardOut 尚未重定向或进程尚未启动.
我想实现的是我想把ls的输出读成一个字符串.
What I want to achieve is that I want to read the output of ls into a string.
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();
推荐答案
在开始进程之前尝试设置标准输出重定向.
Try setting standard output redirection before starting the process.
process.StartInfo.RedirectStandardOutput = true;
process.Start();
这篇关于“StandardOut 尚未重定向或进程尚未启动"在 C# 中读取控制台命令输出时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!