开发者

C# - Process redirected output - Console different to CMD window

开发者 https://www.devze.com 2023-01-21 01:47 出处:网络
I have an application with a Process that use a cmd program. The Process\'s output is redirect like so:

I have an application with a Process that use a cmd program. The Process's output is redirect like so:

pr.StartInfo.RedirectStandardOutput = true;
pr.StartInfo.UseShellExecute = false;
pr.StartInfo.CreateNoWindow = true;                                         
pr.EnableRaisingEvents = true
pr.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pr.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);      
pr.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived); 

The output is then written to the console using:

public void OnDataReceived(object sender, DataReceivedEventArgs e)
{
    if(e.Data != null)                                  
    {
        Console.WriteLine(e.Data);
    }
}

My problem is the Visual Studio prints the output, it's very different to the commandline output. For example, I'm trying to extract data from the output to see how much work has been done. My application output:

0K .......... .......... .......... .......... .......... 1% (开发者_StackOverflow中文版null)

50K .......... .......... .......... .......... .......... 2% (null)

100K .......... .......... .......... .......... .......... 3% (null)

150K .......... .......... .......... .......... .......... 5% (null)

The original Commandline program output (progress bar and percentage accumulates as time goes on):

100%[===================================]

It may not seem a big difference, but for what I'm trying to achieve it is. Why isn't Visual Studio output exactly the same as the CMD out?

Ps. Arguments are the same in both examples.


That's because wget is detecting that you're not running with a visible console. There are arguments with which you can change this.

The reason for this specifically is that the way wget builds the [==== is by overwriting the current line. The output you would see through the redirect would probably be something like this:

  5% [=
 10% [==
 15% [===
 20% [====

etc. All of them on a new line.

You can force this type of feedback by adding --progress=bar to the arguments.


A console and a file is very different. The progress bar clearly uses some special console specific features that are not present when redirecting (which is effectively a file).

0

精彩评论

暂无评论...
验证码 换一张
取 消