c# - 在 C# 中,不能抑制进程的每一行

标签 c# redirectstandardoutput nslookup

我正在尝试将进程的输出读取为字符串。出于某种原因,看起来输出中间的一行似乎被输出了(即,它显示在屏幕上,而不是保存到字符串中)。

string strOutput = "";
Process process = new Process();

process.StartInfo.FileName = "nslookup";
process.StartInfo.Arguments = "-type=mx uic.edu";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();

strOutput = process.StandardOutput.ReadToEnd();

process.WaitForExit();

Console.WriteLine("xxxxxxxxxxxxxxxxxxx");
Console.WriteLine(strOutput);
Console.WriteLine("yyyyyyyyyyyyyyyyyyy");

我得到如下所示的输出:

Non-Authoritative answer:
xxxxxxxxxxxxxxxxxxxx
Server: aaa.myserver.com
Address: 111.222.111.222

uic.edu MX preference = 10, mail exchanger - ...
...
yyyyyyyyyyyyyyyyyyyy

当我通过命令行运行命令时,“Non-Authoritative answer:”出现在“Address: ...”之后

有人可以解释为什么它被输出,而不是作为字符串的一部分存储吗?我可能遗漏了一些明显的东西,但我很困惑。

谢谢

最佳答案

该行可能会转到 STDERR 而不是 STDOUT。尝试重定向标准错误和标准输出。

process.StartInfo.RedirectStandardError = true;

关于c# - 在 C# 中,不能抑制进程的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933737/

相关文章:

c# - 试图将 ffmpeg 的二进制标准输出重定向到 NeroAacEnc 标准输入

c# - StackPanel 中的矩形

c# - 按多列返回 linq 组的类型

java - 使用 "> filename"时不打印到文件的 System.out.println

c# - 当您在 Windows 中运行 git push 命令时,控制台的标准输出重定向会发生什么

c# - 使用 C# 捕获 nslookup shell 输出

dns - dig NS 和 nslookup 提供过时的名称服务器; +trace 或 whois 正确

将圆形鱼眼图像转换为直线图像的 C# 代码

c# - 将 div 元素发送到电子邮件时丢失样式 (C# asp.net)

bash - 是否有从域中检索 NS 记录的 unix 命令?