SimpleExec/Command.cs
/// <summary>
/// Runs a command and reads standard output (stdout).
/// By default, the command line is echoed to standard error (stderr).
/// </summary>
/// <param name="name">The name of the command. This can be a path to an executable file.</param>
/// <param name="args">The arguments to pass to the command.</param>
/// <param name="workingDirectory">The working directory in which to run the command.</param>
/// <param name="noEcho">Whether or not to echo the resulting command line and working directory (if specified) to standard error (stderr).</param>
/// <param name="windowsName">The name of the command to use on Windows only.</param>
/// <param name="windowsArgs">The arguments to pass to the command on Windows only.</param>
/// <param name="echoPrefix">The prefix to use when echoing the command line and working directory (if specified) to standard error (stderr).</param>
/// <param name="configureEnvironment">An action which configures environment variables for the command.</param>
/// <param name="createNoWindow">Whether to run the command in a new window.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the command to exit.</param>
/// <returns>
/// A <see cref="Task{TResult}"/> representing the asynchronous running of the command and reading of standard output (stdout).
/// The task result is a <see cref="string"/> representing the contents of standard output (stdout).
/// </returns>
/// <exception cref="NonZeroExitCodeException">The command exited with non-zero exit code.</exception>
/// <remarks>
/// By default, the resulting command line and the working directory (if specified) are echoed to standard error (stderr).
/// To suppress this behavior, provide the <paramref name="noEcho"/> parameter with a value of <c>true</c>.
/// </remarks>
Runs a command and reads standard output (stdout).
By default, the command line is echoed to standard error (stderr).
Runs a command and reads standard output ( stdout ) . By default the command line is echoed to standard error ( stderr ) .
public static async Task<string> ReadAsync(string name, string args = null, string workingDirectory = null, bool noEcho = false, string windowsName = null, string windowsArgs = null, string echoPrefix = null, Action<IDictionary<string, string>> configureEnvironment = null, bool createNoWindow = false, CancellationToken cancellationToken = default)
        {
            Validate(name);
            using (var process = new Process())
            {
                process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, true, windowsName, windowsArgs, configureEnvironment, createNoWindow);
                var runProcess = process.RunAsync(noEcho, echoPrefix ?? DefaultPrefix.Value, cancellationToken);
                Task<string> readOutput;
                try
                {
                    readOutput = process.StandardOutput.ReadToEndAsync();
                }
                catch (Exception)
                {
                    await runProcess;
                    throw;
                }
                await Task.WhenAll(runProcess, readOutput).ConfigureAwait(false);
                if (process.ExitCode != 0)
                {
                    process.Throw();
                }
                return readOutput.Result;
            }
        }
public static async Task < string > ReadAsync ( string name , string args = null , string workingDirectory = null , bool noEcho = false , string windowsName = null , string windowsArgs = null , string echoPrefix = null , Action < IDictionary < string , string > > configureEnvironment = null , bool createNoWindow = false , CancellationToken cancellationToken = default ) { Validate ( name ) ; using ( var process = new Process ( ) ) { process . StartInfo = ProcessStartInfo . Create ( name , args , workingDirectory , true , windowsName , windowsArgs , configureEnvironment , createNoWindow ) ; var runProcess = process . RunAsync ( noEcho , echoPrefix ?? DefaultPrefix . Value , cancellationToken ) ; Task < string > readOutput ; try { readOutput = process . StandardOutput . ReadToEndAsync ( ) ; } catch ( Exception ) { await runProcess ; throw ; } await Task . WhenAll ( runProcess , readOutput ) . ConfigureAwait ( false ) ; if ( process . ExitCode != 0 ) { process . Throw ( ) ; } return readOutput . Result ; } }
Runs a command and reads standard output (stdout).
Runs a command and reads standard output ( stdout ) .
A  representing the asynchronous running of the command and reading of standard output (stdout).
The task result is a  representing the contents of standard output (stdout).
A representing the asynchronous running of the command and reading of standard output ( stdout ) . The task result is a representing the contents of standard output ( stdout ) .
The command exited with non-zero exit code.
The command exited with non - zero exit code .
name
string
The name of the command. This can be a path to an executable file.
The name of the command . This can be a path to an executable file .
args
string
The arguments to pass to the command.
The arguments to pass to the command .
workingDirectory
string
The working directory in which to run the command.
The working directory in which to run the command .
noEcho
bool
Whether or not to echo the resulting command line and working directory (if specified) to standard error (stderr).
Whether or not to echo the resulting command line and working directory ( if specified ) to standard error ( stderr ) .
windowsName
string
The name of the command to use on Windows only.
The name of the command to use on Windows only .
windowsArgs
string
The arguments to pass to the command on Windows only.
The arguments to pass to the command on Windows only .
echoPrefix
string
The prefix to use when echoing the command line and working directory (if specified) to standard error (stderr).
The prefix to use when echoing the command line and working directory ( if specified ) to standard error ( stderr ) .
configureEnvironment
Action<IDictionary<string, string>>
An action which configures environment variables for the command.
An action which configures environment variables for the command .
createNoWindow
bool
Whether to run the command in a new window.
Whether to run the command in a new window .
cancellationToken
CancellationToken
A  to observe while waiting for the command to exit.
A to observe while waiting for the command to exit .
remarks
By default, the resulting command line and the working directory (if specified) are echoed to standard error (stderr).
To suppress this behavior, provide the  parameter with a value of true
By default the resulting command line and the working directory ( if specified ) are echoed to standard error ( stderr ) . To suppress this behavior provide the parameter with a value of true