C#获取cmd输出,如真实cmd窗口中所示 [英] C# get cmd output just as shown in real cmd window
问题描述
我有一个 BackgroundWorker
线程,该线程运行一个cmd进程并向其中写入多个命令.有些命令可能需要一段时间才能完成,因此我想向用户显示进度的cmd输出.我运行cmd命令的代码如下:
I have a BackgroundWorker
thread that runs a cmd process and writes multiple commands to it. Some of the commands may take a while to complete so I want to show the user the cmd output of the progress.
My code for running the cmd commands looks like this:
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输出不会丢失实际输出.这是我的输出结果:
But the cmd output loos nothing the real output as shown in a regular cmd window. Here is how my output looks like:
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窗口并键入命令时的输出结果:
And here is how output looks like when I open a cmd window and type in the commands:
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#代码中获得此输出?
How do I get this output in my my C# code???
推荐答案
正如@Damien_The_Unbeliever和@ user2033402所建议的那样,似乎由于某种原因,我需要的输出重定向到了 StandardError
而不是到 StandardOutput
.
As @Damien_The_Unbeliever and @user2033402 has suggested, it seems that for some reason the output I needed was redirected to the StandardError
instead of to the StandardOutput
.
所以我的解决方案是以下代码:
So the working solution in my case was this code:
private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
var startInfo = new ProcessStartInfo("cmd.exe")
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
cmd = new Process { StartInfo = startInfo };
cmd.OutputDataReceived += Cmd_OutputDataReceived;
cmd.ErrorDataReceived += Cmd_ErrorDataReceived;
cmd.Start();
cmd.BeginOutputReadLine();
cmd.BeginErrorReadLine();
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;
}));
}
private void Cmd_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Invoke(new Action(() =>
{
txtCmd.Text += e.Data + Environment.NewLine;
}));
}
这篇关于C#获取cmd输出,如真实cmd窗口中所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!