Is it possible to use a C Sharp console as an external console for an executable run through the program?

StackOverflow https://stackoverflow.com/questions/21736014

  •  10-10-2022
  •  | 
  •  

Question

I've got a C# program and I'm wondering if I'm able to launch an external application through my program and use the console to view the output from the external application. Is this possible in C#?

Was it helpful?

Solution

As seen on http://www.dotnetperls.com/redirectstandardoutput:

//
// Setup the process with the ProcessStartInfo class.
//
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"C:\7za.exe"; // Specify exe name.
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
//
// Start the process.
//
using (Process process = Process.Start(start))
{
    //
    // Read in all the text from the process with the StreamReader.
    //
    using (StreamReader reader = process.StandardOutput)
    {
    string result = reader.ReadToEnd();
    Console.Write(result);
    }
}

I've done it myself with Houdini ChessEngine to read PGN files and calculate the board situation with a set of given parameters (WPF application).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top