我有一个 BackgroundWorker
线程,它运行一个 cmd 进程并向它写入多个命令。有些命令可能需要一段时间才能完成,所以我想向用户显示进度的 cmd 输出。
我运行 cmd 命令的代码如下所示:
private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
var startInfo = new ProcessStartInfo("cmd.exe")
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true
};
cmd = new Process { StartInfo = startInfo };
cmd.OutputDataReceived += Cmd_OutputDataReceived;
cmd.Start();
cmd.BeginOutputReadLine();
cmd.StandardInput.WriteLine($"cd {baseFolder}\\External Resources\\img files\\uboot");
string[] commands = CmdCommands.GetUbootFlashCommands();
foreach (var command in commands)
cmd.StandardInput.WriteLine(command);
cmd.StandardInput.WriteLine($"cd {baseFolder}\\External Resources\\img files\\android");
commands = CmdCommands.GetKernelFlashCommands();
foreach (var command in commands)
cmd.StandardInput.WriteLine(command);
cmd.StandardInput.WriteLine("exit");
cmd.WaitForExit();
}
private void Cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Invoke(new Action(() =>
{
txtCmd.Text += e.Data + Environment.NewLine;
}));
}
但是 cmd 输出与常规 cmd 窗口中显示的实际输出没有任何区别。 这是我的输出的样子:
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\>cd C:\img files\uboot
C:\img files\uboot>fastboot flash XXX.bin
C:\img files\uboot>fastboot flash XXX.bin
C:\img files\uboot>cd C:\img files\android
C:\img files\android>fastboot flash kernel
C:\img files\android>fastboot flash system XXX.img
C:\img files\android>fastboot flash userdata XXX.img
C:\img files\android>fastboot flash cache XXX.img
C:\img files\android>exit
下面是当我打开 cmd 窗口并输入命令时输出的样子:
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>cd C:\img files\uboot
C:\img files\uboot>fastboot flash bl2 bl2.bin
target didn't report max-download-size
sending 'bl2' (14 KB)...
OKAY [ 0.006s]
writing 'bl2'...
OKAY [ 0.042s]
finished. total time: 0.052s
C:\img files\uboot>fastboot flash bootloader u-boot.bin
target didn't report max-download-size
sending 'bootloader' (275 KB)...
OKAY [ 0.049s]
writing 'bootloader'...
OKAY [ 0.046s]
finished. total time: 0.098s
C:\img files\uboot>cd C:\img files\android
C:\img files\android>fastboot flash kernel zImage-dtb
target didn't report max-download-size
sending 'kernel' (5099 KB)...
OKAY [ 0.839s]
writing 'kernel'...
OKAY [ 0.145s]
finished. total time: 0.988s
C:\img files\android>fastboot flash system system.img
target didn't report max-download-size
sending 'system' (426874 KB)...
OKAY [ 70.327s]
writing 'system'...
OKAY [ 30.963s]
finished. total time: 101.295s
C:\img files\android>fastboot flash userdata userdata.img
target didn't report max-download-size
sending 'userdata' (35680 KB)...
OKAY [ 5.895s]
writing 'userdata'...
OKAY [ 2.301s]
finished. total time: 8.200s
C:\img files\android>fastboot flash cache cache.img
target didn't report max-download-size
sending 'cache' (6248 KB)...
OKAY [ 1.036s]
writing 'cache'...
OKAY [ 0.380s]
finished. total time: 1.422s
C:\img files\android>
如何在我的 C# 代码中获得此输出???
最佳答案
你能试试这个简单的解决方案吗?我不知道在你的情况下它能成功多少。
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c ping 192.168.x.x"; //or your thing
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
关于C# 获取 cmd 输出,就像在真实 cmd 窗口中显示的一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112187/