Renamed project to GUI
This commit is contained in:
61
GUI/ProcessBuilder.cs
Normal file
61
GUI/ProcessBuilder.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace Password_Manager
|
||||
{
|
||||
public delegate void ProcessSuccess();
|
||||
public delegate void ProcessFailure(Exception e);
|
||||
sealed class ProcessBuilder
|
||||
{
|
||||
public event ProcessSuccess? ProcessFinished;
|
||||
public event ProcessFailure? ProcessFailed;
|
||||
|
||||
public void Run(string procName, string args)
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start(procName, args);
|
||||
ProcessFinished?.Invoke();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProcessFailed?.Invoke(e);
|
||||
}
|
||||
}
|
||||
|
||||
public 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(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
ProcessFailed?.Invoke(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user