Created
March 20, 2020 02:17
-
-
Save edwardrowe/ee34ad5eea62516dd79dbf64f13b6506 to your computer and use it in GitHub Desktop.
Extensions to System.Diagnostics's Process class in C#
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
using System.Diagnostics; | |
using System.Text; | |
public static class ProcessExtensions | |
{ | |
/* Properties ============================================================================================================= */ | |
/* Methods ================================================================================================================ */ | |
/// <summary> | |
/// Runs the specified process and waits for it to exit. Its output and errors are | |
/// returned as well as the exit code from the process. | |
/// See: https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output | |
/// Note that if any deadlocks occur, read the above thread (cubrman's response). | |
/// </summary> | |
public static int Run(this Process process, string application, | |
string arguments, string workingDirectory, out string output, | |
out string errors ) | |
{ | |
process.StartInfo = new ProcessStartInfo | |
{ | |
CreateNoWindow = true, | |
UseShellExecute = false, | |
RedirectStandardError = true, | |
RedirectStandardOutput = true, | |
FileName = application, | |
Arguments = arguments, | |
WorkingDirectory = workingDirectory | |
}; | |
// Use the following event to read both output and errors output. | |
var outputBuilder = new StringBuilder(); | |
var errorsBuilder = new StringBuilder(); | |
process.OutputDataReceived += (_, args) => outputBuilder.AppendLine(args.Data); | |
process.ErrorDataReceived += (_, args) => errorsBuilder.AppendLine(args.Data); | |
// Start the process and wait for it to exit. | |
process.Start(); | |
process.BeginOutputReadLine(); | |
process.BeginErrorReadLine(); | |
process.WaitForExit(); | |
output = outputBuilder.ToString().TrimEnd(); | |
errors = errorsBuilder.ToString().TrimEnd(); | |
return process.ExitCode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment