18 internal class ProcessManager
20 private readonly ProcessStartInfo _winGetStartInfoTemplate;
28 public ProcessManager(
string processName)
30 _winGetStartInfoTemplate =
new ProcessStartInfo()
32 CreateNoWindow =
true,
33 FileName = processName,
34 UseShellExecute =
false,
35 RedirectStandardOutput =
true,
36 StandardOutputEncoding = Encoding.UTF8
50 public ProcessResult ExecuteWingetProcess(
string cmd)
52 return RunProcess(GetStartInfo(cmd));
68 public async Task<ProcessResult> ExecuteWingetProcessAsync(
string cmd, CancellationToken cancellationToken =
default)
70 return await RunProcessAsync(GetStartInfo(cmd), cancellationToken);
82 private ProcessStartInfo GetStartInfo(
string cmd)
84 return new ProcessStartInfo()
86 CreateNoWindow = _winGetStartInfoTemplate.CreateNoWindow,
87 FileName = _winGetStartInfoTemplate.FileName,
88 RedirectStandardOutput = _winGetStartInfoTemplate.RedirectStandardOutput,
89 StandardOutputEncoding = _winGetStartInfoTemplate.StandardOutputEncoding,
90 UseShellExecute = _winGetStartInfoTemplate.UseShellExecute,
91 WindowStyle = _winGetStartInfoTemplate.WindowStyle,
106 private ProcessResult RunProcess(ProcessStartInfo processStartInfo)
108 ProcessResult result =
new();
111 using (Process proc =
new() { StartInfo = processStartInfo })
115 result.Output = proc.StandardOutput.ReadSreamOutputByLine();
126 result.ExitCode = proc.ExitCode;
145 private async Task<ProcessResult> RunProcessAsync(ProcessStartInfo processStartInfo, CancellationToken cancellationToken =
default)
147 ProcessResult result =
new();
150 using (Process proc =
new() { StartInfo = processStartInfo })
154 result.Output = await proc.StandardOutput.ReadSreamOutputByLineAsync(cancellationToken);
157 if (cancellationToken.IsCancellationRequested && !proc.HasExited)
161 result.ExitCode = -1;
175 result.ExitCode = proc.ExitCode;