实时获取命令行输出不适用于所有 cli
Get command line output in real time not working for all cli
下面的代码在 ping whosebug.com
和大多数其他情况下工作正常,但是当我使用 7z.exe 时它不是实时的,它会等到目录被压缩然后显示输出。我用于压缩的参数是a test.7z dirpath
。我还能做点什么吗?
private ProcessStartInfo GetProcessStartInfo(string filename, string arguments)
{
ProcessStartInfo ProcessStartInfo = new ProcessStartInfo();
ProcessStartInfo.CreateNoWindow = true;
ProcessStartInfo.UseShellExecute = false;
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ProcessStartInfo.RedirectStandardOutput = true;
ProcessStartInfo.RedirectStandardError = true;
ProcessStartInfo.RedirectStandardInput = true;
ProcessStartInfo.FileName = filename;
ProcessStartInfo.Arguments = arguments;
}
private void ProcessRun(string filename, string arguments)
{
Process Process = new Process();
Process.StartInfo = GetProcessStartInfo(filename, arguments);
Process.ErrorDataReceived += Process_OutputDataReceived;
Process.OutputDataReceived += Process_OutputDataReceived;
Process.EnableRaisingEvents = true;
Process.Start();
Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
Process.WaitForExit();
}
public ObservableList<string> Output = new ObservableList<string>();
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Output.Add(e.Data);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OutputListBox.ItemsSource = Output;
ProcessRun("cmd.exe", "/c ping whosebug.com");
}
7zip 输出:
7zip 进度:
如果您对百分比感兴趣,那您就走运了——标准输出无法做到这一点。标准输出只处理数据流,而百分比输出实际上是通过直接操作控制台完成的。它不是输出流的一部分 - 无法使用标准 I/O 流复制相同的效果。
所以不是你代码的问题。如果您想仅使用命令提示符查看相同的问题,请尝试 运行 这个:
7z.exe yourarguments > log.txt
>
是输出重定向 - 标准输出不是写入控制台,而是重定向到文件。当您将它与 ping
一起使用时,它会立即打印出标准输出。当您将它与 7zip 一起使用时,您会得到与您的 C# 应用程序相同的结果。
下面的代码在 ping whosebug.com
和大多数其他情况下工作正常,但是当我使用 7z.exe 时它不是实时的,它会等到目录被压缩然后显示输出。我用于压缩的参数是a test.7z dirpath
。我还能做点什么吗?
private ProcessStartInfo GetProcessStartInfo(string filename, string arguments)
{
ProcessStartInfo ProcessStartInfo = new ProcessStartInfo();
ProcessStartInfo.CreateNoWindow = true;
ProcessStartInfo.UseShellExecute = false;
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ProcessStartInfo.RedirectStandardOutput = true;
ProcessStartInfo.RedirectStandardError = true;
ProcessStartInfo.RedirectStandardInput = true;
ProcessStartInfo.FileName = filename;
ProcessStartInfo.Arguments = arguments;
}
private void ProcessRun(string filename, string arguments)
{
Process Process = new Process();
Process.StartInfo = GetProcessStartInfo(filename, arguments);
Process.ErrorDataReceived += Process_OutputDataReceived;
Process.OutputDataReceived += Process_OutputDataReceived;
Process.EnableRaisingEvents = true;
Process.Start();
Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
Process.WaitForExit();
}
public ObservableList<string> Output = new ObservableList<string>();
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Output.Add(e.Data);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OutputListBox.ItemsSource = Output;
ProcessRun("cmd.exe", "/c ping whosebug.com");
}
7zip 输出:
7zip 进度:
如果您对百分比感兴趣,那您就走运了——标准输出无法做到这一点。标准输出只处理数据流,而百分比输出实际上是通过直接操作控制台完成的。它不是输出流的一部分 - 无法使用标准 I/O 流复制相同的效果。
所以不是你代码的问题。如果您想仅使用命令提示符查看相同的问题,请尝试 运行 这个:
7z.exe yourarguments > log.txt
>
是输出重定向 - 标准输出不是写入控制台,而是重定向到文件。当您将它与 ping
一起使用时,它会立即打印出标准输出。当您将它与 7zip 一起使用时,您会得到与您的 C# 应用程序相同的结果。