我有一个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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)