tags:

views:

1812

answers:

1

Duplicate

http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c http://stackoverflow.com/questions/353601/capturing-nslookup-shell-output-with-c

I am looking to call an external program from within my c# code.

The program I am calling, lets say foo.exe returns about 12 lines of text.

I want to call the program and parse thru the output.

What is the most optimal way to do this ?

Code snippet also appreciated :)

Thank You very much.

+4  A: 
using System;
using System.Diagnostics;

public class RedirectingProcessOutput
{
    public static void Main()
    {
        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "/c dir *.cs";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();

        string output = p.StandardOutput.ReadToEnd();

        Console.WriteLine("Output:");
        Console.WriteLine(output);    
    }
}
Stormenet
Almost. You need to call WaitForExit() after ReadToEnd(), to avoid blocking issues.
Michael Petrotta