ProcessWrapper Class

Fluent builder for configuring and running processes. Every With* method mutates the current instance with the replaced value. Every Add* method mutates the current instance with the appended value.

public sealed class ProcessWrapper

Properties

NameDescription
DefaultLogVerbosityGets or sets the default verbosity used in process validation messages and process result string formatting.
DefaultProcessFactoryGets or sets the default process factory used to create process handles.

DefaultLogVerbosity

ProcessWrapper.cs

Gets or sets the default verbosity used in process validation messages and process result string formatting.

public static ProcessLogVerbosity DefaultLogVerbosity { get; set; }

Property value

ProcessLogVerbosity

DefaultProcessFactory

ProcessWrapper.cs

Gets or sets the default process factory used to create process handles.

public static IProcessFactory DefaultProcessFactory { get; set; }

Property value

IProcessFactory

Methods

NameDescription
AddErrorStream(ReadOnlySpan<OutputTarget>)Adds additional error stream handlers.
AddInterceptor(IProcessStartInfoInterceptor)Adds a global process-start-info interceptor.
AddInterceptor(IProcessWrapperInterceptor)Adds a global process-wrapper interceptor.
AddOutputStream(ReadOnlySpan<OutputTarget>)Adds additional output stream handlers.
Create(string)Creates a new ProcessWrapper for the specified executable.
ExecuteAsync(CancellationToken)Starts the process and returns a ProcessInstance immediately. Await the returned instance to wait for the process to exit and get a ProcessResult. Use ProcessInstance.Kill to stop the process explicitly.
ExecuteBufferedAsync(CancellationToken)Starts the process with output buffering and returns a BufferedProcessInstance immediately. Await the returned instance to wait for the process to exit and get a BufferedProcessResult. Use ProcessInstance.Kill to stop the process explicitly.
StartAndForget()Starts the process without tracking it and returns its process ID.
ToString()Returns the command line representation of this process configuration.
WithArguments(IEnumerable<string>)Sets the arguments for the process, replacing any previously set arguments.
WithArguments(ReadOnlySpan<string>)Sets the arguments for the process, replacing any previously set arguments.
WithArguments(string[])Sets the arguments for the process, replacing any previously set arguments.
WithCredentials(string, SecureString, string?)Sets alternate Windows credentials used to start the process.
WithCredentials(string, string, string?)Sets alternate Windows credentials used to start the process.
WithEnvironmentVariables(Action<ProcessWrapperEnvironmentVariables>)Configures environment variables using a callback. Accumulates with previous calls.
WithEnvironmentVariables(IReadOnlyDictionary<string, string?>)Configures environment variables from a dictionary. A null value removes the variable. Accumulates with previous calls.
WithErrorEncoding(Encoding)Sets the encoding used to decode standard error.
WithErrorStream(ReadOnlySpan<OutputTarget>)Replaces all error stream handlers with the specified targets.
WithInputStream(InputSource)Sets the input stream to the specified input source.
WithInterceptor(IProcessStartInfoInterceptor)Sets the process-start-info interceptor for this process wrapper instance.
WithInterceptor(IProcessWrapperInterceptor)Sets the process-wrapper interceptor for this process wrapper instance.
WithKillOnParentExit(bool)Sets whether the child process should be killed when the parent process exits.
WithLimits(ProcessLimits)Sets the process limits, replacing previously configured limits.
WithLimits(Action<ProcessLimits>)Configures process limits. Accumulates with previous calls.
WithLinuxControlGroup(Action<CGroup2>)Configures the Linux cgroup used to apply process limits.
WithLogVerbosity(ProcessLogVerbosity)Sets the verbosity used in process validation messages and process result string formatting.
WithOutputEncoding(Encoding)Sets the encoding used to decode standard output.
WithOutputStream(ReadOnlySpan<OutputTarget>)Replaces all output stream handlers with the specified targets.
WithProcessFactory(IProcessFactory)Sets the process factory for this process wrapper instance.
WithSearchWorkingDirectory(bool)Also looks for the executable in the working directory, before the directories listed in PATH.
WithStartDetached(bool)Sets whether the child process should be detached from the parent process.
WithStartSuspended(bool)Sets whether the child process should start in a suspended state.
WithUseCredentialsForNetworkingOnly(bool)Sets whether to use LOGON_NETCREDENTIALS_ONLY when starting the process on Windows.
WithValidation(ProcessValidationMode)Sets process validation rules.
WithWindowsJobObject(Action<JobObject>)Configures the Windows Job Object used to apply process limits.
WithWorkingDirectory(string)Sets the working directory for the process.

AddErrorStream(ReadOnlySpan<OutputTarget>)

ProcessWrapper.cs

Adds additional error stream handlers.

public ProcessWrapper AddErrorStream(params scoped ReadOnlySpan<OutputTarget> targets)

AddInterceptor overloads

AddInterceptor(IProcessStartInfoInterceptor)

ProcessWrapper.cs

Adds a global process-start-info interceptor.

public static IDisposable AddInterceptor(IProcessStartInfoInterceptor interceptor)
Parameters
interceptor IProcessStartInfoInterceptor
Returns

IDisposable

Remarks

Dispose the returned registration to remove the interceptor.

AddInterceptor(IProcessWrapperInterceptor)

ProcessWrapper.cs

Adds a global process-wrapper interceptor.

public static IDisposable AddInterceptor(IProcessWrapperInterceptor interceptor)
Parameters
interceptor IProcessWrapperInterceptor
Returns

IDisposable

Remarks

Dispose the returned registration to remove the interceptor.

AddOutputStream(ReadOnlySpan<OutputTarget>)

ProcessWrapper.cs

Adds additional output stream handlers.

public ProcessWrapper AddOutputStream(params scoped ReadOnlySpan<OutputTarget> targets)

Create(string)

ProcessWrapper.cs

Creates a new ProcessWrapper for the specified executable.

public static ProcessWrapper Create(string fileName)

Parameters

fileName string

Returns

ProcessWrapper

ExecuteAsync(CancellationToken)

ProcessWrapper.cs

Starts the process and returns a ProcessInstance immediately. Await the returned instance to wait for the process to exit and get a ProcessResult. Use ProcessInstance.Kill to stop the process explicitly.

public ProcessInstance ExecuteAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ProcessInstance

ExecuteBufferedAsync(CancellationToken)

ProcessWrapper.cs

Starts the process with output buffering and returns a BufferedProcessInstance immediately. Await the returned instance to wait for the process to exit and get a BufferedProcessResult. Use ProcessInstance.Kill to stop the process explicitly.

public BufferedProcessInstance ExecuteBufferedAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

BufferedProcessInstance

StartAndForget()

ProcessWrapper.cs

Available in: net11.0(not available in the other target frameworks of the package)

  • Supported on maccatalyst
  • Not supported on ios
  • Not supported on tvos

Starts the process without tracking it and returns its process ID.

[SupportedOSPlatform("maccatalyst")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
public int StartAndForget()

Returns

int

Returns the command line representation of this process configuration.

public override string ToString()

Returns

string

Remarks

The executable path and the arguments are always included, regardless of the verbosity set by ProcessWrapper.WithLogVerbosity. Use ProcessResult.ToString or the message of ProcessExecutionException when the rendering has to honor that verbosity, for example to keep a secret passed as an argument out of the logs.

WithArguments overloads

WithArguments(IEnumerable<string>)

ProcessWrapper.cs

Sets the arguments for the process, replacing any previously set arguments.

public ProcessWrapper WithArguments(IEnumerable<string> arguments)
Parameters
arguments IEnumerable<string>
Returns

ProcessWrapper

WithArguments(ReadOnlySpan<string>)

ProcessWrapper.cs

Sets the arguments for the process, replacing any previously set arguments.

public ProcessWrapper WithArguments(params scoped ReadOnlySpan<string> arguments)
Parameters
arguments ReadOnlySpan<string>
Returns

ProcessWrapper

WithArguments(string[])

ProcessWrapper.cs

Sets the arguments for the process, replacing any previously set arguments.

public ProcessWrapper WithArguments(params string[] arguments)
Parameters
arguments string[]
Returns

ProcessWrapper

WithCredentials overloads

WithCredentials(string, SecureString, string?)

ProcessWrapper.cs
  • Supported on windows

Sets alternate Windows credentials used to start the process.

[SupportedOSPlatform("windows")]
public ProcessWrapper WithCredentials(string userName, SecureString password, string? domain = null)
Parameters
userName string
password SecureString
domain string?
Returns

ProcessWrapper

Remarks

userName can be in the user@domain format when domain is not set.

WithCredentials(string, string, string?)

ProcessWrapper.cs
  • Supported on windows

Sets alternate Windows credentials used to start the process.

[SupportedOSPlatform("windows")]
public ProcessWrapper WithCredentials(string userName, string passwordInClearText, string? domain = null)
Parameters
userName string
passwordInClearText string
domain string?
Returns

ProcessWrapper

Remarks

userName can be in the user@domain format when domain is not set.

WithEnvironmentVariables overloads

WithEnvironmentVariables(Action<ProcessWrapperEnvironmentVariables>)

ProcessWrapper.cs

Configures environment variables using a callback. Accumulates with previous calls.

public ProcessWrapper WithEnvironmentVariables(Action<ProcessWrapperEnvironmentVariables> configure)

WithEnvironmentVariables(IReadOnlyDictionary<string, string?>)

ProcessWrapper.cs

Configures environment variables from a dictionary. A null value removes the variable. Accumulates with previous calls.

public ProcessWrapper WithEnvironmentVariables(IReadOnlyDictionary<string, string?> variables)

WithErrorEncoding(Encoding)

ProcessWrapper.cs

Sets the encoding used to decode standard error.

public ProcessWrapper WithErrorEncoding(Encoding encoding)

Parameters

encoding Encoding

Returns

ProcessWrapper

WithErrorStream(ReadOnlySpan<OutputTarget>)

ProcessWrapper.cs

Replaces all error stream handlers with the specified targets.

public ProcessWrapper WithErrorStream(params scoped ReadOnlySpan<OutputTarget> targets)

WithInputStream(InputSource)

ProcessWrapper.cs

Sets the input stream to the specified input source.

public ProcessWrapper WithInputStream(InputSource source)

Parameters

source InputSource

Returns

ProcessWrapper

WithInterceptor overloads

WithInterceptor(IProcessStartInfoInterceptor)

ProcessWrapper.cs

Sets the process-start-info interceptor for this process wrapper instance.

public ProcessWrapper WithInterceptor(IProcessStartInfoInterceptor interceptor)
Parameters
interceptor IProcessStartInfoInterceptor
Returns

ProcessWrapper

WithInterceptor(IProcessWrapperInterceptor)

ProcessWrapper.cs

Sets the process-wrapper interceptor for this process wrapper instance.

public ProcessWrapper WithInterceptor(IProcessWrapperInterceptor interceptor)
Parameters
interceptor IProcessWrapperInterceptor
Returns

ProcessWrapper

WithKillOnParentExit(bool)

ProcessWrapper.cs

Available in: net11.0(not available in the other target frameworks of the package)

  • Supported on android
  • Supported on linux
  • Supported on windows

Sets whether the child process should be killed when the parent process exits.

[SupportedOSPlatform("android")]
[SupportedOSPlatform("linux")]
[SupportedOSPlatform("windows")]
public ProcessWrapper WithKillOnParentExit(bool killOnParentExit = true)

Parameters

killOnParentExit bool

Returns

ProcessWrapper

WithLimits overloads

WithLimits(ProcessLimits)

ProcessWrapper.cs

Sets the process limits, replacing previously configured limits.

public ProcessWrapper WithLimits(ProcessLimits limits)
Parameters
limits ProcessLimits
Returns

ProcessWrapper

WithLimits(Action<ProcessLimits>)

ProcessWrapper.cs

Configures process limits. Accumulates with previous calls.

public ProcessWrapper WithLimits(Action<ProcessLimits> configure)
Parameters
configure Action<ProcessLimits>
Returns

ProcessWrapper

WithLinuxControlGroup(Action<CGroup2>)

ProcessWrapper.cs

Configures the Linux cgroup used to apply process limits.

public ProcessWrapper WithLinuxControlGroup(Action<CGroup2> configure)

Parameters

configure Action<CGroup2>

Returns

ProcessWrapper

WithLogVerbosity(ProcessLogVerbosity)

ProcessWrapper.cs

Sets the verbosity used in process validation messages and process result string formatting.

public ProcessWrapper WithLogVerbosity(ProcessLogVerbosity verbosity)

Parameters

verbosity ProcessLogVerbosity

Returns

ProcessWrapper

WithOutputEncoding(Encoding)

ProcessWrapper.cs

Sets the encoding used to decode standard output.

public ProcessWrapper WithOutputEncoding(Encoding encoding)

Parameters

encoding Encoding

Returns

ProcessWrapper

WithOutputStream(ReadOnlySpan<OutputTarget>)

ProcessWrapper.cs

Replaces all output stream handlers with the specified targets.

public ProcessWrapper WithOutputStream(params scoped ReadOnlySpan<OutputTarget> targets)

WithProcessFactory(IProcessFactory)

ProcessWrapper.cs

Sets the process factory for this process wrapper instance.

public ProcessWrapper WithProcessFactory(IProcessFactory processFactory)

Parameters

processFactory IProcessFactory

Returns

ProcessWrapper

WithSearchWorkingDirectory(bool)

ProcessWrapper.cs

Also looks for the executable in the working directory, before the directories listed in PATH.

public ProcessWrapper WithSearchWorkingDirectory(bool searchWorkingDirectory = true)

Parameters

searchWorkingDirectory bool

Returns

ProcessWrapper

Remarks

Windows resolves a bare executable name against the current directory before PATH, so this is the native behavior there. POSIX shells deliberately do not.

It is nevertheless disabled by default on every platform, Windows included, for security reasons: a file sitting in the working directory would otherwise take precedence over the command PATH resolves for the same name, letting an untrusted working directory substitute its own executable for the intended one. Only enable it when the working directory is trusted.

WithStartDetached(bool)

ProcessWrapper.cs

Available in: net11.0(not available in the other target frameworks of the package)

Sets whether the child process should be detached from the parent process.

public ProcessWrapper WithStartDetached(bool startDetached = true)

Parameters

startDetached bool

Returns

ProcessWrapper

WithStartSuspended(bool)

ProcessWrapper.cs

Available in: net11.0(not available in the other target frameworks of the package)

  • Supported on macos
  • Supported on windows

Sets whether the child process should start in a suspended state.

[SupportedOSPlatform("macos")]
[SupportedOSPlatform("windows")]
public ProcessWrapper WithStartSuspended(bool startSuspended = true)

Parameters

startSuspended bool

Returns

ProcessWrapper

WithUseCredentialsForNetworkingOnly(bool)

ProcessWrapper.cs
  • Supported on windows

Sets whether to use LOGON_NETCREDENTIALS_ONLY when starting the process on Windows.

[SupportedOSPlatform("windows")]
public ProcessWrapper WithUseCredentialsForNetworkingOnly(bool useCredentialsForNetworkingOnly = true)

Parameters

useCredentialsForNetworkingOnly bool

Returns

ProcessWrapper

Remarks

When enabled, this behaves similarly to runas /netonly.

WithValidation(ProcessValidationMode)

ProcessWrapper.cs

Sets process validation rules.

public ProcessWrapper WithValidation(ProcessValidationMode mode)

WithWindowsJobObject(Action<JobObject>)

ProcessWrapper.cs

Configures the Windows Job Object used to apply process limits.

public ProcessWrapper WithWindowsJobObject(Action<JobObject> configure)

Parameters

configure Action<JobObject>

Returns

ProcessWrapper

WithWorkingDirectory(string)

ProcessWrapper.cs

Sets the working directory for the process.

public ProcessWrapper WithWorkingDirectory(string workingDirectory)

Parameters

workingDirectory string

Returns

ProcessWrapper

Operators

NameDescription
operator |(ProcessWrapper, ProcessWrapper)Creates a pipeline between 2 commands.

operator |(ProcessWrapper, ProcessWrapper)

ProcessWrapper.cs

Creates a pipeline between 2 commands.

public static ProcessPipeline operator |(ProcessWrapper left, ProcessWrapper right)