【发布时间】:2021-08-13 06:14:02
【问题描述】:
我尝试了其他问题的所有解决方案,但都没有奏效。我还尝试在 CMD 中“调用 filename.exe>log.txt”,但没有成功。如果您能帮我解决这个问题,我将不胜感激。
我是一个非英语的学生,所以表达可能会很奇怪。感谢您的理解。
using (Process process = new Process())
{
process.StartInfo.FileName = ProcessPath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(ProcessPath);
process.Start();
while (process.HasExited)
{
TextBox1.AppendText(process.StandardOutput.ReadLine()+"\r\n");
}
process.WaitForExit();
}
【问题讨论】:
-
How do I get output from a command to appear in a control on a Form in real-time? -- 阅读关于
SynchronizingObject属性的说明。 -
不起作用 不是一个有用的问题描述!
-
while (process.HasExited)是一个无限循环。状态不能再更改为 false。而不是这个,你可以process.StandardOutput.ReadToEnd();这是一个阻塞调用,它一直等到进程完成。 (见docs.microsoft.com/en-us/dotnet/api/…) -
我不太明白你的意思是什么输出?是否要显示生成的名为 log.txt 的文件的文本?