Add system extension for Process

This commit is contained in:
Webber 2020-04-21 22:54:34 +02:00 committed by Webber Takken
parent e53dcf13e3
commit 328b0d8ac0
3 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b5da3bd7e18c43d79243410166c8dc9a
timeCreated: 1587493708

View File

@ -0,0 +1,42 @@
using System.Diagnostics;
using System.Text;
public static class ProcessExtensions
{
// Execute an application or binary with given arguments
//
// See: https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output
public static int Run(this Process process, string application,
string arguments, string workingDirectory, out string output,
out string errors)
{
// Configure how to run the application
process.StartInfo = new ProcessStartInfo {
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
FileName = application,
Arguments = arguments,
WorkingDirectory = workingDirectory
};
// Read the output
var outputBuilder = new StringBuilder();
var errorsBuilder = new StringBuilder();
process.OutputDataReceived += (_, args) => outputBuilder.AppendLine(args.Data);
process.ErrorDataReceived += (_, args) => errorsBuilder.AppendLine(args.Data);
// Run the application and wait for it to complete
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
// Format the output
output = outputBuilder.ToString().TrimEnd();
errors = errorsBuilder.ToString().TrimEnd();
return process.ExitCode;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 29c1880a390c4af7be821b7877602815
timeCreated: 1587494270