简体   繁体   English

C# - 处理重定向输出 - 与CMD窗口不同的控制台

[英]C# - Process redirected output - Console different to CMD window

I have an application with a Process that use a cmd program. 我有一个使用cmd程序的Process的应用程序。 The Process's output is redirect like so: Process的输出是重定向,如下所示:

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. 我的问题是Visual Studio打印输出,它与命令行输出非常不同。 For example, I'm trying to extract data from the output to see how much work has been done. 例如,我正在尝试从输出中提取数据以查看已完成的工作量。 My application output: 我的应用输出:

0K .......... .......... .......... .......... .......... 1% (null) 0K .......... .......... .......... .......... ......... .1%(null)
50K .......... .......... .......... .......... .......... 2% (null) 50K .......... .......... .......... .......... ......... 。2%(null)
100K .......... .......... .......... .......... .......... 3% (null) 100K .......... .......... .......... .......... ......... .3%(null)
150K .......... .......... .......... .......... .......... 5% (null) 150K .......... .......... .......... .......... ......... .5%(null)

The original Commandline program output (progress bar and percentage accumulates as time goes on): 原始Commandline程序输出(进度条和百分比随着时间的推移累积):
100%[===================================] 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? 为什么Visual Studio输出与CMD输出完全不同?

Ps. PS。 Arguments are the same in both examples. 两个示例中的参数都相同。

That's because wget is detecting that you're not running with a visible console. 那是因为wget检测到你没有使用可见的控制台运行。 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. 具体的原因是wget构建[====是通过覆盖当前行。 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. 您可以通过在参数中添加--progress=bar来强制执行此类反馈。

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). 进度条显然使用了一些特殊的控制台特定功能,这些功能在重定向时(实际上是文件)不存在。

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

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