【发布时间】:2019-05-11 23:30:00
【问题描述】:
我正在尝试逐行获取流程的输出,但我得到了返回的空值。你能检查我下面的代码,让我知道我在这里做错了什么吗?请推荐
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(),它会显示所有行。
【问题讨论】:
-
在while中使用断点,检查该行是否真的有值?
-
您不是在等待该过程完成吗?
-
@HEWhoDoesn'tKnow:当我使用 ReadLine() 时它没有任何价值。
-
@Amy 对不起,我在复制粘贴中错过了它。刚刚更新
-
为什么不等它退出在得到它的输出?
标签: c#