Table of Contents

Class OsUtils

Namespace
VpnHood.Net.Toolkit.Utils
Assembly
VpnHood.Net.Toolkit.dll

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

fileName string

The program.

command string

The arguments, as one string.

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

fileName string

The program.

command string

The arguments, as one string.

timeout TimeSpan

How 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

fileName string

The program.

command string

The arguments, as one string.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<string>

A task whose result is the program's standard output.

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

cancellationToken was 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

command string

The 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

command string

The command line.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<string>

A task whose result is the command's standard output.

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

cancellationToken was canceled.