Show / Hide Table of Contents

Class ProcessUtils

Provides methods for launching child processes.

Inheritance
Object
ProcessUtils
Namespace: NanoByte.Common
Assembly: NanoByte.Common.dll
Syntax
public static class ProcessUtils : Object

Methods

AsAdmin(ProcessStartInfo)

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

Declaration
public static ProcessStartInfo AsAdmin(this ProcessStartInfo startInfo)
Parameters
Type Name Description
ProcessStartInfo startInfo
Returns
Type Description
ProcessStartInfo
Exceptions
Type Condition
PlatformNotSupportedException

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

Assembly(String, String[])

Creates a ProcessStartInfo for launching an assembly located in InstallBase.

Declaration
public static ProcessStartInfo Assembly(string name, params string[] arguments)
Parameters
Type Name Description
String name

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

String[] arguments

The command-line arguments to pass to the assembly.

Returns
Type Description
ProcessStartInfo
Exceptions
Type Condition
FileNotFoundException

The specified assembly could not be found.

Deconstruct(ProcessStartInfo, out String, out String)

Deconstructs a ProcessStartInfo like a tuple.

Declaration
public static void Deconstruct(this ProcessStartInfo startInfo, out string fileName, out string arguments)
Parameters
Type Name Description
ProcessStartInfo startInfo
String fileName
String arguments

EscapeArgument(String)

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

Declaration
public static string EscapeArgument(this string value)
Parameters
Type Name Description
String value
Returns
Type Description
String

FromCommandLine(String)

Converts a command-line into a start info.

Declaration
public static ProcessStartInfo FromCommandLine(string commandLine)
Parameters
Type Name Description
String commandLine
Returns
Type Description
ProcessStartInfo

JoinEscapeArguments(IEnumerable<String>)

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

Declaration
public static string JoinEscapeArguments(this IEnumerable<string> parts)
Parameters
Type Name Description
IEnumerable<String> parts

The strings to be combined.

Returns
Type Description
String

Run(ProcessStartInfo)

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

Declaration
public static int Run(this ProcessStartInfo startInfo)
Parameters
Type Name Description
ProcessStartInfo startInfo
Returns
Type Description
Int32

The exit code of the process.

Exceptions
Type Condition
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.

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

Declaration
public static Process Start(this ProcessStartInfo startInfo)
Parameters
Type Name Description
ProcessStartInfo startInfo
Returns
Type Description
Process

The newly launched process.

Exceptions
Type Condition
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, String[])

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

Declaration
public static Process Start(string fileName, params string[] arguments)
Parameters
Type Name Description
String fileName

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

String[] arguments

The command-line arguments to pass to the executable.

Returns
Type Description
Process

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

Exceptions
Type Condition
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.

Declaration
public static string ToCommandLine(this ProcessStartInfo startInfo)
Parameters
Type Name Description
ProcessStartInfo startInfo
Returns
Type Description
String

WaitForExitCode(Process)

Waits for a running Process to complete.

Declaration
public static int WaitForExitCode(this Process process)
Parameters
Type Name Description
Process process
Returns
Type Description
Int32

The exit code of the process.

WaitForSuccess(Process)

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

Declaration
public static void WaitForSuccess(this Process process)
Parameters
Type Name Description
Process process
Exceptions
Type Condition
ExitCodeException

The process exited with a non-zero ExitCode.

In This Article
Back to top Copyright Bastian Eicher