我正在使用 VBOXMANAGE 来“导出”来宾机器。VBOXManage 是一个控制台应用程序,可以从主机控制来宾机器的行为。由于导出命令是一个很长的过程,它返回的过程更新如下:
0%...10%...20%...30%...100%
我正在编写一个 C# 应用程序,它将使用 Process 调用 VBOXManage。这是我的代码:
Process VBOXProc = new Process();
VBOXProc.StartInfo.FileName = VBOXMANAGE;
VBOXProc.StartInfo.Arguments = Arguments;
VBOXProc.StartInfo.UseShellExecute = false;
VBOXProc.StartInfo.CreateNoWindow = true;
VBOXProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
VBOXProc.StartInfo.RedirectStandardError = true;
VBOXProc.StartInfo.RedirectStandardOutput = true;
VBOXProc.OutputDataReceived += new DataReceivedEventHandler(VBOXProc_OutputDataReceived);
VBOXProc.ErrorDataReceived += new DataReceivedEventHandler(VBOXProc_ErrorDataReceived);
VBOXProc.EnableRaisingEvents = true;
VBOXProc.Start();
VBOXProc.BeginOutputReadLine();
VBOXProc.BeginErrorReadLine();
VBOXProc.WaitForExit();
这很好,除了输出是按 LINE 读取的。这意味着流程更新“0%...10%...20%...30%...100%”只会在实际流程完成后显示。
有没有办法实时捕获控制台输出?
谢谢!