Class OsUtils
Provides the means to run a program or a shell command and read its output.
public static class OsUtils
- Inheritance
-
OsUtils
- Inherited Members
Methods
ExecuteCommand(string, string)
Runs a program with its arguments, and waits for it.
public static string ExecuteCommand(string fileName, string command)
Parameters
Returns
- string
The program's standard output.
Exceptions
- ExternalException
The command ended with an exit code other than 0, which is the exception's error code; its error output, with the command line, is the message.
- Win32Exception
The program cannot be started.
ExecuteCommand(string, string, TimeSpan)
Runs a program with its arguments, and waits for it at most a timeout, after which it is killed; reading the rest of its output then is not timed.
public static string ExecuteCommand(string fileName, string command, TimeSpan timeout)
Parameters
fileNamestringThe program.
commandstringThe arguments, as one string.
timeoutTimeSpanHow long to wait, or InfiniteTimeSpan for no limit.
Returns
- string
The program's standard output.
Exceptions
- TimeoutException
The program was still running at the timeout, or its output had not ended, as a child it left behind holds it open; both are ended.
- ArgumentOutOfRangeException
The timeout is negative, other than infinite, or too long.
- ExternalException
The command ended with an exit code other than 0, which is the exception's error code; its error output, with the command line, is the message.
- Win32Exception
The program cannot be started.
ExecuteCommandAsync(string, string, CancellationToken)
Runs a program with its arguments, and waits for it asynchronously.
public static Task<string> ExecuteCommandAsync(string fileName, string command, CancellationToken cancellationToken)
Parameters
fileNamestringThe program.
commandstringThe arguments, as one string.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Remarks
A cancellation stops the wait, not the program.
Exceptions
- ExternalException
The command ended with an exit code other than 0, which is the exception's error code; its error output, with the command line, is the message.
- Win32Exception
The program cannot be started.
- OperationCanceledException
cancellationTokenwas canceled.
ExecuteShellCommand(string)
Runs a command line in bash, given to it as one argument, as written, and waits for it.
public static string ExecuteShellCommand(string command)
Parameters
commandstringThe command line.
Returns
- string
The command's standard output.
Exceptions
- ExternalException
The command ended with an exit code other than 0, which is the exception's error code; its error output, with the command line, is the message.
- Win32Exception
The program cannot be started.
ExecuteShellCommandAsync(string, CancellationToken)
Runs a command line in bash, as ExecuteShellCommand(string) does, and waits for it asynchronously.
public static Task<string> ExecuteShellCommandAsync(string command, CancellationToken cancellationToken)
Parameters
commandstringThe command line.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Remarks
A cancellation stops the wait, not the program.
Exceptions
- ExternalException
The command ended with an exit code other than 0, which is the exception's error code; its error output, with the command line, is the message.
- Win32Exception
The program cannot be started.
- OperationCanceledException
cancellationTokenwas canceled.