Getting started
editInstall the package
Add the Meziantou.Framework.ProcessWrapper package to a project:
dotnet package add Meziantou.Framework.ProcessWrapperThe 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);- ProcessWrapper.Create takes the executable: a name that is looked up in
PATH, or a path. - WithArguments sets the arguments. Each argument is passed to the process as is: there is nothing to quote or escape.
- ExecuteAsync starts the process and returns a ProcessInstance at once. Awaiting it waits for the process to exit and returns a ProcessResult with the exit code, the process id, and the start and exit dates.
- By default, a non-zero exit code throws a ProcessExecutionException. See Exit codes and errors.
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
- Configuring the process: the working directory, the environment variables and the other options.
- Reading the output: stream the output while the process runs.
- Testing: run your code without starting real processes.