Table of Contents

Class ProcessUtils

Namespace
NanoByte.Common
Assembly
NanoByte.Common.dll

Provides methods for launching child processes.

public static class ProcessUtils
Inheritance
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, 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 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

FromCommandLine(string)

Converts a command-line into a start info.

[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

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, such 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.

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, 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 into a command-line with proper escaping.

[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.