Class ProcessUtils
Provides methods for launching child processes.
public static class ProcessUtils
- Inheritance
-
objectProcessUtils
Methods
AsAdmin(ProcessStartInfo)
Modifies a System.Diagnostics.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 System.Diagnostics.ProcessStartInfo for launching an assembly located in InstallBase.
[Pure]
public static ProcessStartInfo Assembly(string name, IEnumerable<string> arguments)
Parameters
name
stringThe 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 could not be found.
Assembly(string, params string[])
Creates a System.Diagnostics.ProcessStartInfo for launching an assembly located in InstallBase.
[Pure]
public static ProcessStartInfo Assembly(string name, params string[] arguments)
Parameters
name
stringThe 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 System.Diagnostics.ProcessStartInfo like a tuple.
[Pure]
public static void Deconstruct(this ProcessStartInfo startInfo, out string fileName, out string arguments)
Parameters
startInfo
ProcessStartInfofileName
stringarguments
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 System.Diagnostics.Process and waits for it to complete. Handles and wraps System.ComponentModel.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 System.Diagnostics.ProcessStartInfo.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 System.Diagnostics.ProcessStartInfo.EnvironmentVariables to avoid System.ArgumentExceptions.
Start(ProcessStartInfo)
Starts a new System.Diagnostics.Process and runs it in parallel with this one. Handles and wraps System.ComponentModel.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 System.Diagnostics.ProcessStartInfo.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 System.Diagnostics.Process and runs it in parallel with this one. Handles and wraps System.ComponentModel.Win32Exceptions.
public static Process Start(string fileName, IEnumerable<string> arguments)
Parameters
fileName
stringThe 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 System.Diagnostics.ProcessStartInfo.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 System.Diagnostics.Process and runs it in parallel with this one. Handles and wraps System.ComponentModel.Win32Exceptions.
public static Process Start(string fileName, params string[] arguments)
Parameters
fileName
stringThe 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 System.Diagnostics.ProcessStartInfo.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 System.Diagnostics.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 System.Diagnostics.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 System.Diagnostics.Process.ExitCode.