Last active
August 12, 2022 18:04
-
-
Save kyrathasoft/fceb8fbc82b25180dcb3acd26664e88c to your computer and use it in GitHub Desktop.
Launch process and capture output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
===== Capturing Output From A Console Process ===== | |
This example program relies upon hello_world.exe being in the same directory as this source code's | |
compiled output, which is directory C:\Users\kyrat\Dropbox\csdev\examples\ as of 12 Aug 2022 | |
*/ | |
using System; | |
using System.Diagnostics; | |
namespace CaptureOutputFromConsole{ | |
class CaptureOutput { | |
static void Main() { | |
/* | |
The compiled program hello_world.exe should NOT use a Console.ReadKey() or ConsoleReadLine() -- which | |
sometimes are used to pause a console program so that the user can read output before the program exits | |
Here is the very brief source code used to produce our hello_world.exe program: | |
using System; | |
namespace HelloWorld { | |
class Howdy { | |
static void Main(){ | |
Console.WriteLine(" Howdy, world!"); | |
// Console.ReadKey(); removed so that capture-output.cs can function | |
} | |
} | |
} | |
*/ | |
var proc = new Process { | |
StartInfo = new ProcessStartInfo { | |
FileName = @"hello_world.exe", | |
UseShellExecute = false, | |
RedirectStandardOutput = true, | |
CreateNoWindow = true | |
} | |
}; | |
proc.Start(); | |
string p = proc.StandardOutput.ReadToEnd(); | |
proc.WaitForExit(); | |
Console.WriteLine("Captured output: " + p); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment