Table of Contents

Class ProcessUtils

Namespace
NanoByte.Common
Assembly
NanoByte.Common.dll

Provides methods for launching child processes.

public static class ProcessUtils
Inheritance
object
ProcessUtils

Methods

AsAdmin(ProcessStartInfo)

Modifies a ProcessStartInfo to request elevation to Administrator on Windows using UAC.

public static ProcessStartInfo AsAdmin(this ProcessStartInfo startInfo)

Parameters

startInfo ProcessStartInfo

Returns

ProcessStartInfo

Exceptions

PlatformNotSupportedException

The current operating system does not support UAC or it is disabled.

Assembly(string, IEnumerable<string>)

Creates a ProcessStartInfo for launching an assembly located in InstallBase.

[Pure]
public static ProcessStartInfo Assembly(string name, IEnumerable<string> arguments)

Parameters

name string

The name of the assembly to launch (without the file extension).

arguments IEnumerable<string>

The command-line arguments to pass to the assembly.

Returns

ProcessStartInfo

Exceptions

FileNotFoundException

The specified assembly or the dotnet host needed to launch it could not be found.

Assembly(string, params string[])

Creates a ProcessStartInfo for launching an assembly located in InstallBase.

[Pure]
public static ProcessStartInfo Assembly(string name, params string[] arguments)

Parameters

name string

The name of the assembly to launch (without the file extension).

arguments string[]

The command-line arguments to pass to the assembly.

Returns

ProcessStartInfo

Exceptions

FileNotFoundException

The specified assembly or the dotnet host needed to launch it could not be found.

Deconstruct(ProcessStartInfo, out string, out string)

Deconstructs a ProcessStartInfo like a tuple.

[Pure]
public static void Deconstruct(this ProcessStartInfo startInfo, out string fileName, out string arguments)

Parameters

startInfo ProcessStartInfo
fileName string
arguments string

EscapeArgument(string)

Escapes a string for use as a command-line argument, making sure it is encapsulated within " if it contains whitespace characters.

[Pure]
public static string EscapeArgument(this string value)

Parameters

value string

Returns

string

EscapeArgumentPosix(string)

Escapes a string for use as an argument on a POSIX shell command-line, making sure it is encapsulated within " unless it consists solely of characters without a special meaning.

[Pure]
public static string EscapeArgumentPosix(this string value)

Parameters

value string

Returns

string

Remarks

The result is a single shell word, which can be concatenated with other words without whitespace in between. Not suitable for the first word of a command-line, see JoinEscapeArgumentsPosix(IEnumerable<string>).

FromCommandLine(string)

Converts a command-line into a start info using the rules of the current platform.

[Pure]
public static ProcessStartInfo FromCommandLine(string commandLine)

Parameters

commandLine string

Returns

ProcessStartInfo

JoinEscapeArguments(IEnumerable<string>)

Combines multiple strings into one for use as a command-line argument using EscapeArgument(string).

[Pure]
public static string JoinEscapeArguments(this IEnumerable<string> parts)

Parameters

parts IEnumerable<string>

The strings to be combined.

Returns

string

JoinEscapeArgumentsPosix(IEnumerable<string>)

Combines multiple strings into one for use as a command-line in a POSIX shell using EscapeArgumentPosix(string).

[Pure]
public static string JoinEscapeArgumentsPosix(this IEnumerable<string> parts)

Parameters

parts IEnumerable<string>

The strings to be combined. The first one is the command to run.

Returns

string

Run(ProcessStartInfo)

Starts a new Process and waits for it to complete. Handles and wraps Win32Exceptions.

public static int Run(this ProcessStartInfo startInfo)

Parameters

startInfo ProcessStartInfo

Returns

int

The exit code of the process.

Exceptions

IOException

There was a problem launching the executable.

FileNotFoundException

The executable file could not be found.

NotAdminException

The target process requires elevation but the UAC prompt could not be displayed because UseShellExecute is false.

OperationCanceledException

The user was asked for intervention by the OS (e.g. a UAC prompt) and the user cancelled.

SanitizeEnvironmentVariables()

Workaround for environment variable problems, like variable names that differ only in casing when running on Windows.

public static void SanitizeEnvironmentVariables()

Remarks

Call this before any access to EnvironmentVariables to avoid ArgumentExceptions.

SplitArguments(string?)

Splits a string of arguments, without a leading program name, into individual arguments using the rules of the current platform.

[Pure]
public static string[] SplitArguments(string? arguments)

Parameters

arguments string

The arguments to be split.

Returns

string[]

An array of individual arguments.

Remarks

On Windows this follows the rules of CommandLineToArgvW. On other platforms this follows POSIX shell quoting rules, without performing any expansions, so </code> and ' have a special meaning there.

Start(ProcessStartInfo)

Starts a new Process and runs it in parallel with this one. Handles and wraps Win32Exceptions.

public static Process Start(this ProcessStartInfo startInfo)

Parameters

startInfo ProcessStartInfo

Returns

Process

The newly launched process.

Exceptions

IOException

There was a problem launching the executable.

FileNotFoundException

The executable file could not be found.

NotAdminException

The target process requires elevation but the UAC prompt could not be displayed because UseShellExecute is false.

OperationCanceledException

The user was asked for intervention by the OS (e.g. a UAC prompt) and the user cancelled.

Start(string, IEnumerable<string>)

Starts a new Process and runs it in parallel with this one. Handles and wraps Win32Exceptions.

public static Process Start(string fileName, IEnumerable<string> arguments)

Parameters

fileName string

The path of the file to open or executable to launch.

arguments IEnumerable<string>

The command-line arguments to pass to the executable.

Returns

Process

The newly launched process; null if an existing process was reused.

Exceptions

IOException

There was a problem launching the executable.

FileNotFoundException

The executable file could not be found.

NotAdminException

The target process requires elevation but the UAC prompt could not be displayed because UseShellExecute is false.

OperationCanceledException

The user was asked for intervention by the OS (e.g. a UAC prompt) and the user cancelled.

Start(string, params string[])

Starts a new Process and runs it in parallel with this one. Handles and wraps Win32Exceptions.

public static Process Start(string fileName, params string[] arguments)

Parameters

fileName string

The path of the file to open or executable to launch.

arguments string[]

The command-line arguments to pass to the executable.

Returns

Process

The newly launched process; null if an existing process was reused.

Exceptions

IOException

There was a problem launching the executable.

FileNotFoundException

The executable file could not be found.

NotAdminException

The target process requires elevation but the UAC prompt could not be displayed because UseShellExecute is false.

OperationCanceledException

The user was asked for intervention by the OS (e.g. a UAC prompt) and the user cancelled.

ToCommandLine(ProcessStartInfo)

Converts a start info into a command-line with proper escaping for the current platform, which FromCommandLine(string) can convert back.

[Pure]
public static string ToCommandLine(this ProcessStartInfo startInfo)

Parameters

startInfo ProcessStartInfo

Returns

string

WaitForExitCode(Process)

Waits for a running Process to complete.

public static int WaitForExitCode(this Process process)

Parameters

process Process

Returns

int

The exit code of the process.

WaitForSuccess(Process)

Waits for a running Process to complete with an exit code of zero.

public static void WaitForSuccess(this Process process)

Parameters

process Process

Exceptions

ExitCodeException

The process exited with a non-zero ExitCode.