Initial commit
This commit is contained in:
103
MkvToolnixWrapper/Utils/ProcessRunner.cs
Normal file
103
MkvToolnixWrapper/Utils/ProcessRunner.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace MkvPropEditWrapper.Utils;
|
||||
|
||||
public sealed class ProcessRunner
|
||||
{
|
||||
private readonly ProcessStartInfo _startInfo;
|
||||
|
||||
public ProcessRunner(ProcessStartInfo startInfo)
|
||||
{
|
||||
_startInfo = startInfo;
|
||||
|
||||
_startInfo.RedirectStandardOutput = true;
|
||||
_startInfo.RedirectStandardError = true;
|
||||
_startInfo.StandardOutputEncoding = Encoding.UTF8;
|
||||
_startInfo.StandardErrorEncoding = Encoding.UTF8;
|
||||
}
|
||||
|
||||
public bool TryRun(out string output)
|
||||
{
|
||||
using var process = Process.Start(_startInfo);
|
||||
|
||||
var outputBuilder = new StringBuilder();
|
||||
var errorBuilder = new StringBuilder();
|
||||
|
||||
if (process is null)
|
||||
{
|
||||
output = "";
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
process.OutputDataReceived += (_, args) =>
|
||||
{
|
||||
outputBuilder.AppendLine(args.Data);
|
||||
};
|
||||
|
||||
process.ErrorDataReceived += (_, args) =>
|
||||
{
|
||||
errorBuilder.AppendLine(args.Data);
|
||||
};
|
||||
|
||||
process.BeginOutputReadLine();
|
||||
process.BeginErrorReadLine();
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
if (process.ExitCode != 0)
|
||||
{
|
||||
output = BuildError(process, outputBuilder, errorBuilder);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
output = outputBuilder.ToString();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<(bool sucess, string output)> TryRunAsync()
|
||||
{
|
||||
using var process = Process.Start(_startInfo);
|
||||
|
||||
var outputBuilder = new StringBuilder();
|
||||
var errorBuilder = new StringBuilder();
|
||||
|
||||
if (process is null)
|
||||
{
|
||||
return (false, "");
|
||||
}
|
||||
|
||||
process.OutputDataReceived += (_, args) =>
|
||||
{
|
||||
outputBuilder.AppendLine(args.Data);
|
||||
};
|
||||
|
||||
process.ErrorDataReceived += (_, args) =>
|
||||
{
|
||||
errorBuilder.AppendLine(args.Data);
|
||||
};
|
||||
|
||||
process.BeginOutputReadLine();
|
||||
process.BeginErrorReadLine();
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
return process.ExitCode != 0
|
||||
? (false, BuildError(process, outputBuilder, errorBuilder))
|
||||
: (true, outputBuilder.ToString());
|
||||
}
|
||||
|
||||
private static string BuildError(Process process, StringBuilder output, StringBuilder error)
|
||||
{
|
||||
StringBuilder builder = new();
|
||||
|
||||
builder.AppendLine($"Error while running process '{process.StartInfo.FileName} {process.StartInfo.Arguments}'\n");
|
||||
builder.AppendLine($"Standard output:\n{output.ToString()}\n");
|
||||
builder.AppendLine($"Standard error:\n{error.ToString()}\n");
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user