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/Password Manager/ProcessBuilder.cs

85 lines
1.6 KiB
C#
Raw Normal View History

using System.Diagnostics;
2023-03-31 07:16:27 +02:00
using System.Text;
namespace Password_Manager
{
public delegate void ProcessSuccess();
public delegate void ProcessFailure(Exception e);
static class ProcessBuilder
{
public event ProcessSuccess? ProcessFinished;
public event ProcessFailure? ProcessFailed;
public static void Run(string procName, string args)
{
try
{
Process.Start(procName, args);
ProcessFinished?.Invoke();
}
catch (Exception e)
{
ProcessFailed?.Invoke(e);
}
ClearDelegate()
}
private void ClearDelegate()
{
//clear delegate because static
if (ProcessFinished != null)
{
foreach (ProcessSuccess p in ProcessFinished)
{
ProcessFinished -= p;
}
}
if (ProcessFailed != null)
{
foreach (ProcessFailure p in ProcessFailed)
{
ProcessFailed -= p;
}
}
}
public static string? GetOutput(string procName, string args)
{
try
{
Process proc = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = procName,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true;
CreateNoWindow = true;
}
};
proc.Start();
StringBuilder builder = new StringBuilder();
while (!proc.StandardOutput.EndOfStream)
{
builder.Append(proc.StandardOutput.ReadLine());
}
ProcessFinished?.Invoke();
return builder.ToString();
} catch (Exception e)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ProcessFailed.Invoke(e);
}
ClearDelegate();
return null;
}
}
}