简体   繁体   English

实时获取命令行输出不适用于所有CLI

[英]Get command line output in real time not working for all cli

The below code is working fine for ping stackoverflow.com and most other cases but when I using 7z.exe it's not real time, it waits until directory is compressed and then shows the output. 下面的代码对于ping stackoverflow.com和大多数其他情况都可以正常工作,但是当我使用7z.exe时,它不是实时的,它会等到目录压缩后才显示输出。 The argument that I used for compress is a test.7z dirpath . 我用于compress的参数是a test.7z dirpath Can I do anything else? 我还能做别的吗?

    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 stackoverflow.com");
    }

7zip Output: 7zip输出:

在此处输入图片说明

7zip Progress: 7zip进度:

在此处输入图片说明

If you're interested in the percentage, you're out of luck - that isn't done by standard output. 如果您对百分比感兴趣,那么您就不走运了-这不是标准输出所能完成的。 Standard output only handles streams of data, while the percentage output is actually done by manipulating the console directly. 标准输出仅处理数据流,而百分比输出实际上是通过直接操纵控制台来完成的。 It's not part of the output stream - there is no way to replicate the same effect using the standard I/O streams. 它不是输出流的一部分-无法使用标准I / O流来复制相同的效果。

So it's not a problem of your code. 因此,这不是您的代码问题。 If you want to see the same problem using just the Command prompt, try running this: 如果仅使用命令提示符就希望看到相同的问题,请尝试运行以下命令:

7z.exe yourarguments > log.txt

The > is an output redirect - instead of writing to the console, the standard output is redirected to a file. >是输出重定向-将标准输出重定向到文件,而不是写入控制台。 When you use it with ping , it immediately prints out standard output as it comes. 当与ping一起使用时,它会立即输出标准输出。 When you use it with 7zip, you get the same result as with your C# application. 当将它与7zip一起使用时,您将获得与C#应用程序相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM