如何在 WINForm 中获取 robocopy(或其他 cmd 程序)输出?

Yon*_*ing 5 c# cmd winforms

我有一个 GUI 程序,它将在该 GUI 程序中调用 cmd。就我而言,GUI 调用 robocopy 将文件复制到文件服务器。我想在 GUI 中显示进度。那么我怎样才能获得 robocopy 的输出并将其显示在我的 GUI 上。

最好的问候, 邢永伟

Meh*_*ari 3

使用StandardOutput流:

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;

// ...
// read from process.StandardOutput
Run Code Online (Sandbox Code Playgroud)

StandardOutput或者,您可以调用传递一个回调,而不是直接读取,BeginOutputReadLine该回调会告诉您何时输出新行。