使用 C# 捕獲進程輸出

使用 C# 捕獲進程輸出

Intro

不少時候咱們可能會須要執行一段命令獲取一個輸出,遇到的比較典型的就是以前咱們須要用 FFMpeg 實現視頻的編碼壓縮水印等一系列操做,當時使用的是 FFMpegCore 這個類庫,這個類庫的實現原理是啓動另一個進程,啓動 ffmpeg 並傳遞相應的處理參數,並根據進程輸出獲取處理進度git

爲了方便使用,實現了兩個幫助類來方便的獲取進程的輸出,分別是 ProcessExecutorCommandRunner,前者更爲靈活,能夠經過事件添加本身的額外事件訂閱處理,後者爲簡化版,主要是隻獲取輸出的場景,二者的實現原理大致是同樣的,啓動一個 Process,並監聽其輸出事件獲取輸出github

ProcessExecutor

使用示例,這個示例是獲取保存 nuget 包的路徑的一個示例:async

using var executor = new ProcessExecutor("dotnet", "nuget locals global-packages -l");
var folder = string.Empty;
executor.OnOutputDataReceived += (sender, str) =>
{
    if(str is null)
        return;

    Console.WriteLine(str);

    if(str.StartsWith("global-packages:"))
    {
        folder = str.Substring("global-packages:".Length).Trim();                    
    }
};
executor.Execute();

Console.WriteLine(folder);

ProcessExecutor 實現代碼以下:this

public class ProcessExecutor : IDisposable
{
    public event EventHandler<int> OnExited;

    public event EventHandler<string> OnOutputDataReceived;

    public event EventHandler<string> OnErrorDataReceived;

    protected readonly Process _process;

    protected bool _started;

    public ProcessExecutor(string exePath) : this(new ProcessStartInfo(exePath))
    {
    }

    public ProcessExecutor(string exePath, string arguments) : this(new ProcessStartInfo(exePath, arguments))
    {
    }

    public ProcessExecutor(ProcessStartInfo startInfo)
    {
        _process = new Process()
        {
            StartInfo = startInfo,
            EnableRaisingEvents = true,
        };
        _process.StartInfo.UseShellExecute = false;
        _process.StartInfo.CreateNoWindow = true;
        _process.StartInfo.RedirectStandardOutput = true;
        _process.StartInfo.RedirectStandardInput = true;
        _process.StartInfo.RedirectStandardError = true;
    }

    protected virtual void InitializeEvents()
    {
        _process.OutputDataReceived += (sender, args) =>
        {
            if (args.Data != null)
            {
                OnOutputDataReceived?.Invoke(sender, args.Data);
            }
        };
        _process.ErrorDataReceived += (sender, args) =>
        {
            if (args.Data != null)
            {
                OnErrorDataReceived?.Invoke(sender, args.Data);
            }
        };
        _process.Exited += (sender, args) =>
        {
            if (sender is Process process)
            {
                OnExited?.Invoke(sender, process.ExitCode);
            }
            else
            {
                OnExited?.Invoke(sender, _process.ExitCode);
            }
        };
    }

    protected virtual void Start()
    {
        if (_started)
        {
            return;
        }
        _started = true;

        _process.Start();
        _process.BeginOutputReadLine();
        _process.BeginErrorReadLine();
        _process.WaitForExit();
    }

    public async virtual Task SendInput(string input)
    {
        try
        {
            await _process.StandardInput.WriteAsync(input!);
        }
        catch (Exception e)
        {
            OnErrorDataReceived?.Invoke(_process, e.ToString());
        }
    }

    public virtual int Execute()
    {
        InitializeEvents();
        Start();
        return _process.ExitCode;
    }

    public virtual async Task<int> ExecuteAsync()
    {
        InitializeEvents();
        return await Task.Run(() =>
        {
            Start();
            return _process.ExitCode;
        }).ConfigureAwait(false);
    }

    public virtual void Dispose()
    {
        _process.Dispose();
        OnExited = null;
        OnOutputDataReceived = null;
        OnErrorDataReceived = null;
    }
}

CommandExecutor

上面的這種方式比較靈活但有些繁瑣,因而有了下面這個版本編碼

使用示例:code

[Fact]
public void HostNameTest()
{
    if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        return;
    }

    var result = CommandRunner.ExecuteAndCapture("hostname");

    var hostName = Dns.GetHostName();
    Assert.Equal(hostName, result.StandardOut.TrimEnd());
    Assert.Equal(0, result.ExitCode);
}

實現源碼:orm

public static class CommandRunner
{
    public static int Execute(string commandPath, string arguments = null, string workingDirectory = null)
    {
        using var process = new Process()
        {
            StartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty)
            {
                UseShellExecute = false,
                CreateNoWindow = true,

                WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory
            }
        };

        process.Start();
        process.WaitForExit();
        return process.ExitCode;
    }

    public static CommandResult ExecuteAndCapture(string commandPath, string arguments = null, string workingDirectory = null)
    {
        using var process = new Process()
        {
            StartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty)
            {
                UseShellExecute = false,
                CreateNoWindow = true,

                RedirectStandardOutput = true,
                RedirectStandardError = true,

                WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory
            }
        };
        process.Start();
        var standardOut = process.StandardOutput.ReadToEnd();
        var standardError = process.StandardError.ReadToEnd();
        process.WaitForExit();
        return new CommandResult(process.ExitCode, standardOut, standardError);
    }
}

public sealed class CommandResult
{
    public CommandResult(int exitCode, string standardOut, string standardError)
    {
        ExitCode = exitCode;
        StandardOut = standardOut;
        StandardError = standardError;
    }

    public string StandardOut { get; }
    public string StandardError { get; }
    public int ExitCode { get; }
}

More

若是隻要執行命令獲取是否執行成功則使用 CommandRunner.Execute 便可,只獲取輸出和是否成功能夠用 CommandRunner.ExecuteAndCapture 方法,若是想要進一步的添加事件訂閱則使用 ProcessExecutor視頻

Reference

相關文章
相關標籤/搜索