运行控制台进程后尝试获取StandardOutput [英] Trying to get StandardOutput after running a console process

查看:39
本文介绍了运行控制台进程后尝试获取StandardOutput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下C#代码运行控制台进程。目标也是从该过程收集所有输出:

I can run a console process using the following C# code. The goal is also to collect all the output from such process:

System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.StartInfo.Arguments = commandLine;
proc.StartInfo.FileName = "signtool.exe";
proc.StartInfo.RedirectStandardOutput = true;

proc.StartInfo.UseShellExecute = false;

proc.Start();

if (proc.WaitForExit(10000))
{
    Debug.WriteLine(proc.StandardOutput.ReadToEnd());
}

我收到的是这个东西:


完成添加其他存储区

"Done Adding Additional Store\r\n"

但是当我从Windows命令行执行相同操作,我得到以下信息:

But when I do the same from a Windows command line I get this:

Done Adding Additional Store
SignTool Error: File not found: C:\SomeBadFile.exe

为什么我的代码只得到第一行输出?

Why am I getting only the first line of output with my code?

推荐答案

看到您缺少的行似乎是一条错误消息,如果您不应该查看 Process.StandardError属性

Seeing as the line you are missing seems like an error message, should you not be looking at Process.StandardError Property


当进程将文本写入其标准错误流时,该文本通常是在控制台上显示的
。通过重定向StandardError
流,您可以操纵或抑制进程的错误输出。
例如,您可以过滤文本,使用不同的格式或将
的输出写入控制台和指定的日志文件。

When a Process writes text to its standard error stream, that text is normally displayed on the console. By redirecting the StandardError stream, you can manipulate or suppress the error output of a process. For example, you can filter the text, format it differently, or write the output to both the console and a designated log file.

这篇关于运行控制台进程后尝试获取StandardOutput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆