Files
OfficeTool/Services/ProcessRunner.cs
hariel1985 486589f9bc v1.16 — Remove PowerShell edition, restore flat project structure
PowerShell version removed — will use OV code signing certificate instead.
Files moved back from src/ to project root.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 07:07:27 +02:00

64 sor
2.0 KiB
C#

using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace InstaSoftOfficeTool.Services
{
public class ProcessRunner
{
public event Action<string> OutputReceived;
public async Task<int> RunAsync(string fileName, string arguments, bool redirectOutput = true)
{
var psi = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = redirectOutput,
RedirectStandardError = redirectOutput,
StandardOutputEncoding = redirectOutput ? Encoding.UTF8 : null,
StandardErrorEncoding = redirectOutput ? Encoding.UTF8 : null
};
using (var process = new Process { StartInfo = psi })
{
if (redirectOutput)
{
process.OutputDataReceived += (s, e) =>
{
if (e.Data != null)
OutputReceived?.Invoke(e.Data);
};
process.ErrorDataReceived += (s, e) =>
{
if (e.Data != null)
OutputReceived?.Invoke("[HIBA] " + e.Data);
};
}
process.Start();
if (redirectOutput)
{
process.BeginOutputReadLine();
process.BeginErrorReadLine();
}
await Task.Run(() => process.WaitForExit());
return process.ExitCode;
}
}
public async Task<string> RunAndCaptureAsync(string fileName, string arguments)
{
var sb = new StringBuilder();
OutputReceived += line => sb.AppendLine(line);
await RunAsync(fileName, arguments);
return sb.ToString();
}
}
}