Search code examples
c#redirectstandardoutputnslookup

in c#, can't suppress every line of a process


I'm trying to read the output of a process to string. For some reason, it sort of looks like the one line in the middle of the output seems to get outputted (ie, it's displayed on the screen, and NOT saved to the string).

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

I get output that looks like this:

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

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

When I run the command via command line, "Non-Authoritative answer:" comes after "Address: ..."

Can someone explain why it's outputted, and not stored as part of the string? I'm probably missing something obvious, but I'm boggled.

Thanks


Solution

  • That line is probably going to STDERR rather than STDOUT. Try redirecting standard error as well as standard output.

    process.StartInfo.RedirectStandardError = true;