Getting started

edit

Install the package

Add the Meziantou.Framework.ProcessWrapper package to a project:

dotnet package add Meziantou.Framework.ProcessWrapper

The examples of this documentation omit the using directives: they need using Meziantou.Framework;.

Run a command

// Execute and wait for exit (throws if exit code is non-zero by default)
var result = await ProcessWrapper.Create("dotnet")
    .WithArguments("--version")
    .ExecuteAsync();

Assert.True(result.ExitCode.IsSuccess);

Capture the output

ExecuteBufferedAsync also collects the lines the process writes to standard output and standard error:

// Capture all output
var result = await ProcessWrapper.Create("dotnet")
    .WithArguments("--info")
    .ExecuteBufferedAsync();

int exitCode = result.ExitCode;
Assert.Equal(0, exitCode);

// Access output after awaiting
Assert.Contains(result.Output.StandardOutput, line => line.Text.StartsWith(".NET SDK", StringComparison.Ordinal));

result.Output is a ProcessOutputCollection: every line, in the order it was received, with the stream it comes from and the time it was received. StandardOutput and StandardError filter it by stream. To process the lines while the process runs instead, see Reading the output.

The builder changes in place

The With* and Add* methods change the ProcessWrapper they are called on, and return it so you can chain the calls. They do not create a copy: two variables that reference the same ProcessWrapper see the same configuration. Create one ProcessWrapper per command, or configure it completely before sharing it.

You can execute the same ProcessWrapper several times: each ExecuteAsync call starts a new process.

Next steps