2023-03-29 14:36:16 +02:00
|
|
|
using System.Diagnostics;
|
2023-03-31 07:16:27 +02:00
|
|
|
using System.Text;
|
2023-03-29 14:36:16 +02:00
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
namespace Common;
|
|
|
|
|
|
|
|
|
|
public delegate void ProcessSuccess();
|
|
|
|
|
public delegate void ProcessFailure(Exception e);
|
2023-04-02 22:48:05 +02:00
|
|
|
public sealed class ProcessBuilder
|
2023-03-29 14:36:16 +02:00
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
public event ProcessSuccess? ProcessFinished;
|
|
|
|
|
public event ProcessFailure? ProcessFailed;
|
2023-03-29 14:36:16 +02:00
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
public void Run(string procName, string args)
|
|
|
|
|
{
|
|
|
|
|
try
|
2023-03-29 14:36:16 +02:00
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
Process.Start(procName, args);
|
|
|
|
|
ProcessFinished?.Invoke();
|
2023-03-29 14:36:16 +02:00
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
catch (Exception e)
|
2023-03-31 07:18:38 +02:00
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
ProcessFailed?.Invoke(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string? GetOutput(string procName, string args)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process proc = new Process()
|
2023-03-29 14:36:16 +02:00
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
StartInfo = new ProcessStartInfo
|
2023-03-29 14:36:16 +02:00
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
FileName = procName,
|
|
|
|
|
Arguments = args,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
CreateNoWindow = true
|
2023-03-29 14:36:16 +02:00
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proc.Start();
|
2023-03-29 14:36:16 +02:00
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
while (!proc.StandardOutput.EndOfStream)
|
2023-03-29 14:36:16 +02:00
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
builder.Append(proc.StandardOutput.ReadLine());
|
2023-03-29 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
ProcessFinished?.Invoke();
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
} catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
ProcessFailed?.Invoke(e);
|
2023-03-29 14:36:16 +02:00
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
|
|
|
|
|
return null;
|
2023-03-29 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
}
|