This repository has been archived on 2025-09-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Password-Manager-Legacy/Common/ProcessBuilder.cs

60 lines
1.1 KiB
C#
Raw Normal View History

using System.Diagnostics;
2023-03-31 07:16:27 +02:00
using System.Text;
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-04-02 22:10:42 +02:00
public event ProcessSuccess? ProcessFinished;
public event ProcessFailure? ProcessFailed;
2023-04-02 22:10:42 +02:00
public void Run(string procName, string args)
{
try
{
2023-04-02 22:10:42 +02:00
Process.Start(procName, args);
ProcessFinished?.Invoke();
}
2023-04-02 22:10:42 +02:00
catch (Exception e)
{
2023-04-02 22:10:42 +02:00
ProcessFailed?.Invoke(e);
}
}
public string? GetOutput(string procName, string args)
{
try
{
Process proc = new Process()
{
2023-04-02 22:10:42 +02:00
StartInfo = new ProcessStartInfo
{
2023-04-02 22:10:42 +02:00
FileName = procName,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
2023-04-02 22:10:42 +02:00
};
proc.Start();
2023-04-02 22:10:42 +02:00
StringBuilder builder = new StringBuilder();
while (!proc.StandardOutput.EndOfStream)
{
2023-04-02 22:10:42 +02:00
builder.Append(proc.StandardOutput.ReadLine());
}
2023-04-02 22:10:42 +02:00
ProcessFinished?.Invoke();
return builder.ToString();
} catch (Exception e)
{
ProcessFailed?.Invoke(e);
}
2023-04-02 22:10:42 +02:00
return null;
}
}