我试图逐行获取过程的输出,但是我正在返回空值。您能否检查下面的代码,让我知道这里做错了什么?请建议
Process process = new Process();
process.StartInfo.FileName = dirPath + "\\test.exe";
process.StartInfo.Arguments = "-a -h -s " + dir;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
using (StreamReader streamReader = Process.StandardOutput)
{
string line = streamReader.ReadLine();
while (line != null)
{
Console.WriteLine(line);
}
}
process.WaitForExit();
如果我使用string line = streamReader.ReadToEnd()
,它将显示所有行。
答案 0 :(得分:0)
通过以下更改,我得以解决此问题。我需要在while循环中添加!streamReader.EndOfStream,然后使用对我有用的streamReader.ReadLine()逐行解析。
Process process = new Process();
process.StartInfo.FileName = Pathdir + "\\test.exe";
process.StartInfo.Arguments = "-a -h -s " + dir;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
using (StreamReader streamReader = process.StandardOutput)
{
while (!streamReader.EndOfStream)
{
string line = streamReader.ReadLine();
Console.WriteLine(line);
}
}