Class RemoteProcess
Represents a remote process.
public sealed class RemoteProcess : IDisposable
- Inheritance
-
RemoteProcess
- Implements
- Inherited Members
Remarks
This class enables interacting with the remote process, such as reading its output streams, writing to its input stream, and retrieving the exit code.
If you won't be writing to the input stream, call WriteEof() to prevent blocking if the remote process prompts for input.
The class provides methods to read from standard output and standard error, and write to standard input using bytes, characters, strings, lines, or Streams.
When the output is read, you can call the GetExitCodeAsync(CancellationToken) method to retrieve the exit code. If there was any remaining output from the remote process it will be discarded.
Properties
ExecutionAborted
Gets a cancellation token that is canceled when operations that depend on the remote process should stop.
public CancellationToken ExecutionAborted { get; }
Property Value
HasTerminal
Returns whether the process was started with a terminal.
public bool HasTerminal { get; }
Property Value
StandardInputStream
Returns a Stream for writing to the process standard input.
public Stream StandardInputStream { get; }
Property Value
Remarks
Disposing/Closing the Stream calls WriteEof().
StandardInputWriter
Returns a StreamWriter for writing to the process standard input.
public StreamWriter StandardInputWriter { get; }
Property Value
Methods
Dispose()
Disposes the SSH channel that executes the process.
public void Dispose()
GetExitCodeAsync(CancellationToken)
Waits for the process to exit and returns the exit code. Any remaining output is discarded.
public ValueTask<int> GetExitCodeAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
GetExitStatusAsync(CancellationToken)
Waits for the process to exit and returns the exit code and signal. If there is pending output it is discarded.
public ValueTask<RemoteProcess.ExitStatus> GetExitStatusAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- ValueTask<RemoteProcess.ExitStatus>
The exit status containing the exit code and exit signal (if terminated by signal).
ReadAllLinesAsync(bool, bool, CancellationToken)
Reads all lines from the process output asynchronously.
public IAsyncEnumerable<(bool IsError, string Content)> ReadAllLinesAsync(bool readStdout, bool readStderr, CancellationToken cancellationToken = default)
Parameters
readStdoutboolWhether to read standard output.
readStderrboolWhether to read standard error.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- IAsyncEnumerable<(bool IsError, string Content)>
An async enumerable of lines with stdout or stderr indicator.
Remarks
After using this method, methods that read bytes or streams can no longer be used.
ReadAllLinesAsync(CancellationToken)
Reads all lines from the process output asynchronously.
public IAsyncEnumerable<(bool IsError, string Content)> ReadAllLinesAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- IAsyncEnumerable<(bool IsError, string Content)>
An async enumerable of lines with stdout or stderr indicator.
Remarks
Both standard output and standard error are read. To control this, use the overload that accepts readStdout and readStderr arguments.
After using this method, methods that read bytes or streams can no longer be used.
ReadAsStream(StderrHandler)
Reads the process output as a Stream.
public Stream ReadAsStream(StderrHandler stderrHandler)
Parameters
stderrHandlerStderrHandlerStderrHandler for standard error output.
Returns
Remarks
After using this method, no other read methods can be used.
ReadAsStreamReader(StderrHandler, int)
Reads the process output as a StreamReader.
public StreamReader ReadAsStreamReader(StderrHandler stderrHandler, int bufferSize = -1)
Parameters
stderrHandlerStderrHandlerStderrHandler for standard error output.
bufferSizeintBuffer size for the StreamReader.
Returns
- StreamReader
A StreamReader for reading standard output.
Remarks
After using this method, no other read methods can be used.
ReadAsync(Memory<byte>?, Memory<byte>?, CancellationToken)
Reads process output as bytes.
public ValueTask<(bool IsError, int BytesRead)> ReadAsync(Memory<byte>? stdoutBuffer, Memory<byte>? stderrBuffer, CancellationToken cancellationToken = default)
Parameters
stdoutBufferMemory<byte>?Buffer for standard output.
stderrBufferMemory<byte>?Buffer for standard error.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- ValueTask<(bool IsError, int BytesRead)>
A tuple indicating if data is from stdout or stderr and the amount of bytes read.
ReadAsync(Memory<char>?, Memory<char>?, CancellationToken)
Reads process output as characters.
public ValueTask<(bool IsError, int BytesRead)> ReadAsync(Memory<char>? stdoutBuffer, Memory<char>? stderrBuffer, CancellationToken cancellationToken = default)
Parameters
stdoutBufferMemory<char>?Buffer for standard output.
stderrBufferMemory<char>?Buffer for standard error.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- ValueTask<(bool IsError, int BytesRead)>
A tuple indicating if data is from stdout or stderr and the amount of characters read.
Remarks
After using this method, methods that read bytes or streams can no longer be used.
ReadLineAsync(bool, bool, CancellationToken)
Reads a single line from the process output.
public ValueTask<(bool IsError, string? Content)> ReadLineAsync(bool readStdout, bool readStderr, CancellationToken cancellationToken = default)
Parameters
readStdoutboolWhether to read standard output.
readStderrboolWhether to read standard error.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- ValueTask<(bool IsError, string Content)>
A tuple with the line and whether it is from standard error. Line is null when the process has exited and all output has been read.
Remarks
After using this method, methods that read bytes or streams can no longer be used.
ReadLineAsync(CancellationToken)
Reads a single line from the process output.
public ValueTask<(bool IsError, string? Content)> ReadLineAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- ValueTask<(bool IsError, string Content)>
A tuple with the line and whether it is from standard error. Line is null when the process has exited and all output has been read.
Remarks
Both standard output and standard error are read. To control this, use the overload that accepts readStdout and readStderr arguments.
After using this method, methods that read bytes or streams can no longer be used.
ReadToEndAsStringAsync(bool, bool, CancellationToken)
Reads all output until the process exits and returns it as strings.
public ValueTask<(string StandardOutput, string StandardError)> ReadToEndAsStringAsync(bool readStdout, bool readStderr, CancellationToken cancellationToken = default)
Parameters
readStdoutboolWhether to read standard output.
readStderrboolWhether to read standard error.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- ValueTask<(string StandardOutput, string StandardError)>
A tuple containing stdout and stderr as strings.
Remarks
After using this method, methods that read bytes or streams can no longer be used.
ReadToEndAsStringAsync(CancellationToken)
Reads all output until the process exits and returns it as strings.
public ValueTask<(string StandardOutput, string StandardError)> ReadToEndAsStringAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- ValueTask<(string StandardOutput, string StandardError)>
A tuple containing stdout and stderr as strings.
Remarks
Both standard output and standard error are read. To control this, use the overload that accepts readStdout and readStderr arguments.
After using this method, methods that read bytes or streams can no longer be used.
ReadToEndAsync(Func<Memory<byte>, object?, CancellationToken, ValueTask>?, object?, Func<Memory<byte>, object?, CancellationToken, ValueTask>?, object?, CancellationToken)
Reads all output until the process exits using custom handlers.
public ValueTask ReadToEndAsync(Func<Memory<byte>, object?, CancellationToken, ValueTask>? handleStdout, object? stdoutContext, Func<Memory<byte>, object?, CancellationToken, ValueTask>? handleStderr, object? stderrContext, CancellationToken cancellationToken = default)
Parameters
handleStdoutFunc<Memory<byte>, object, CancellationToken, ValueTask>Handler for standard output data.
stdoutContextobjectContext passed to the stdout handler.
handleStderrFunc<Memory<byte>, object, CancellationToken, ValueTask>Handler for standard error data.
stderrContextobjectContext passed to the stderr handler.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
ReadToEndAsync(Stream?, Stream?, CancellationToken)
Writes all output to Streams until the process exits.
public ValueTask ReadToEndAsync(Stream? stdoutStream, Stream? stderrStream, CancellationToken cancellationToken = default)
Parameters
stdoutStreamStreamStream to write standard output to.
stderrStreamStreamStream to write standard error to.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
SendSignal(string)
Sends a signal to the process.
public bool SendSignal(string signalName)
Parameters
signalNamestringThe signal name (e.g., "TERM", "KILL").
Returns
SetTerminalSize(int, int)
Sets the terminal window size.
public bool SetTerminalSize(int width, int height)
Parameters
Returns
WriteAsync(ReadOnlyMemory<byte>, CancellationToken)
Writes bytes to the process standard input.
public ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<byte>The buffer to write.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
WriteAsync(ReadOnlyMemory<char>, CancellationToken)
Writes characters to the process standard input.
public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<char>The buffer to write.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
WriteAsync(string, CancellationToken)
Writes a string to the process standard input.
public ValueTask WriteAsync(string value, CancellationToken cancellationToken = default)
Parameters
valuestringThe string to write.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
WriteEof()
Writes end-of-file to the process standard input.
public void WriteEof()
WriteLineAsync(ReadOnlyMemory<char>, CancellationToken)
Writes a line of characters to the process standard input.
public ValueTask WriteLineAsync(ReadOnlyMemory<char> buffer = default, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<char>The buffer to write.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
WriteLineAsync(string?, CancellationToken)
Writes a line of text to the process standard input.
public ValueTask WriteLineAsync(string? value, CancellationToken cancellationToken = default)
Parameters
valuestringThe string to write.
cancellationTokenCancellationTokenToken to cancel the operation.