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

发布于 2024-09-29 16:07:40 字数 1448 浏览 4 评论 0原文

我有一个带有 Process 的应用程序,它使用 cmd 程序。进程的输出像这样重定向:

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); 

然后使用以下方式将输出写入控制台:

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

我的问题是 Visual Studio 打印输出,它与命令行输出非常不同。例如,我试图从输出中提取数据以查看已完成多少工作。我的应用程序输出:

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

原始命令行程序输出(进度条和百分比随着时间的推移而累积):
100%[===================================]

看起来可能差别不大,但是为了什么我正在努力实现它。为什么 Visual Studio 的输出与 CMD 的输出不完全相同?

诗。两个示例中的参数相同。

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% (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事还在继续 2024-10-06 16:07:40

这是因为 wget 检测到您没有使用可见控制台运行。您可以通过一些参数来改变这一点。

具体原因是 wget 构建 [==== 的方式是覆盖当前行。您通过重定向看到的输出可能是这样的:

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

等等。所有这些都在一个新行上。

您可以通过向参数添加 --progress=bar 来强制提供此类反馈。

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.

寻找一个思念的角度 2024-10-06 16:07:40

控制台和文件有很大不同。进度条显然使用了一些特殊的控制台特定功能,这些功能在重定向时不存在(实际上是一个文件)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文