simple-exec icon indicating copy to clipboard operation
simple-exec copied to clipboard

🏃 A .NET library that runs external commands.

SimpleExec

SimpleExec

NuGet version

Build status CodeQL analysis lint Spell check

SimpleExec is a .NET library that runs external commands. It wraps System.Diagnostics.Process to make things easier.

SimpleExec intentionally does not invoke the system shell.

Platform support: .NET 6.0 and later.

  • Quick start
  • Run
  • Read
  • Other optional arguments
  • Exceptions

Quick start

using static SimpleExec.Command;
Run("foo", "arg1 arg2");

Run

Run("foo");
Run("foo", "arg1 arg2");
Run("foo", new[] { "arg1", "arg2" });

await RunAsync("foo");
await RunAsync("foo", "arg1 arg2");
await RunAsync("foo", new[] { "arg1", "arg2" });

By default, the command is echoed to standard output (stdout) for visibility.

Read

var (standardOutput1, standardError1) = await ReadAsync("foo");
var (standardOutput2, standardError2) = await ReadAsync("foo", "arg1 arg2");
var (standardOutput3, standardError3) = await ReadAsync("foo", new[] { "arg1", "arg2" });

Other optional arguments

string workingDirectory = "",
bool noEcho = false,
string? echoPrefix = null,
Action<IDictionary<string, string?>>? configureEnvironment = null,
bool createNoWindow = false,
Encoding? encoding = null,
Func<int, bool>? handleExitCode = null,
string? standardInput = null,
bool cancellationIgnoresProcessTree = false,
CancellationToken cancellationToken = default,

Exceptions

If the command has a non-zero exit code, an ExitCodeException is thrown with an int ExitCode property and a message in the form of:

$"The process exited with code {ExitCode}."

In the case of ReadAsync, an ExitCodeReadException is thrown, which inherits from ExitCodeException, and has string Out and Error properties, representing standard out (stdout) and standard error (stderr), and a message in the form of:

$@"The process exited with code {ExitCode}.

Standard Output:

{Out}

Standard Error:

{Error}"

Overriding default exit code handling

Most programs return a zero exit code when they succeed and a non-zero exit code fail. However, there are some programs which return a non-zero exit code when they succeed. For example, Robocopy returns an exit code less than 8 when it succeeds and 8 or greater when a failure occurs.

The throwing of exceptions for specific non-zero exit codes may be suppressed by passing a delegate to handleExitCode which returns true when it has handled the exit code and default exit code handling should be suppressed, and returns false otherwise.

For example, when running Robocopy, exception throwing should be suppressed for an exit code less than 8:

Run("ROBOCOPY", "from to", handleExitCode: code => code < 8);

Note that it may be useful to record the exit code. For example:

var exitCode = 0;
Run("ROBOCOPY", "from to", handleExitCode: code => (exitCode = code) < 8);

// see https://ss64.com/nt/robocopy-exit.html
var oneOrMoreFilesCopied = exitCode & 1;
var extraFilesOrDirectoriesDetected = exitCode & 2;
var misMatchedFilesOrDirectoriesDetected = exitCode & 4;

Run by Gregor Cresnar from the Noun Project.