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
  •  | 
  •  

문제

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#?

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top