1

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

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

当我通过命令行运行命令时,“非权威答案:”出现在“地址:...”之后

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

谢谢

4

1 回答 1

5

那条线可能会转到 STDERR 而不是 STDOUT。尝试重定向标准错误以及标准输出。

process.StartInfo.RedirectStandardError = true;
于 2015-01-14T00:06:08.097 回答