Run shell commands using C# and get the info into string [duplicate]

You can redirect the output with ProcessStartInfo. There’s examples on MSDN and SO.

E.G.

Process proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "program.exe",
        Arguments = "command line arguments to your executable",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

then start the process and read from it:

proc.Start();
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    // do something with line
}

Depending on what you are trying to accomplish you can achieve a lot more as well. I’ve written apps that asynchrously pass data to the command line and read from it as well. Such an example is not easily posted on a forum.

Leave a Comment