2

I have a C# application that needs to show certain data via SNMP. I am using the Net-SNMP on Windows and running the command

snmpwalk -Os -c <community_string> -v 2c <hostname> system

To get all variables under system.

On the command prompt it shows a line-by-line output for each variable retrieved. But when I run it via C# it shows all output at once using the following code. How can I get the output dynamically i.e. line by line as in command prompt.

        Process proc = new Process();
        proc.StartInfo.FileName = "snmpwalk.exe";
        proc.StartInfo.Arguments = "-Os -C <community_string> -v 2c <hostname> system";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.OutputDataReceived += (sender, e) => Console.WriteLine("{0}", e.Data);
        proc.Start();
        proc.BeginOutputReadLine();

The same code when replaced for a command like ping shows a dynamic line-by-line output.

Aneesh Relan
  • 342
  • 3
  • 12
  • Almost certainly unrelated, but do you have a call to `proc.WaitForExit` somewhere? Without that there's a race if it writes all its output too quickly. – Cameron Jun 22 '16 at 18:20
  • 1
    Hmm, I seem to recall something about the CRT buffering output differently (i.e. not by line) when it detects output is going to a pipe instead of the console; that could be it. See http://stackoverflow.com/a/2909123/21475 – Cameron Jun 22 '16 at 18:59
  • There is no `WaitForExit` call anywhere. No solutions work on the given link, – Aneesh Relan Jun 23 '16 at 04:34
  • Alright, you'll need a call to `WaitForExit` to make sure all the standard output is collected -- it has to be done when using `BeginOutputReadLine` (unless you don't care about occasionally missing some output). But it has nothing to do with your question. – Cameron Jun 23 '16 at 16:06

0 Answers0