Table of Contents

Class ExeHelper

Namespace
Heleonix.Execution
Assembly
Heleonix.Execution.dll

Provides functionality for working with executables.

public static class ExeHelper
Inheritance
ExeHelper
Inherited Members

Methods

Execute(string, string, bool, string, int)

Executes an executable by the specified exePath.

public static ExeResult Execute(string exePath, string arguments, bool extractOutput, string workingDirectory = "", int waitForExit = 2147483647)

Parameters

exePath string

The path to the executable file to run.

arguments string

Command line arguments to pass into the executable.

extractOutput bool

Defines whether to redirect and extract standard output and errors or not.

workingDirectory string

The working directory to launch the executable in.

waitForExit int

A number of millisecoonds to wait for the process ending. Use MaxValue to wait infinitely.

Returns

ExeResult

An exit result.

Examples

var result = ExeHelper.Execute(
@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
"--app=http://www.google.com --window-size=300,300 --new-window",
true,
string.Empty,
2000);

Console.WriteLine(result.ExitCode); // An exit code: value returned by `Main` or by `Environment.Exit(exitCode)` etc.
Console.WriteLine(result.Output); // Output like `Console.WriteLine` is available here
Console.WriteLine(result.Error); // Output like `Console.Error.WriteLine` is available here.

Exceptions

InvalidOperationException

See the inner exception for details.

Execute(string, string, TextWriter, TextWriter, string, int, int)

Executes an executable by the specified exePath. Asynchronously forwards StandardOutput and StandardError of the executable to the specified outputWriter and errorWriter using the intermediate char buffer with the specified bufferSize.

public static int Execute(string exePath, string arguments, TextWriter outputWriter, TextWriter errorWriter, string workingDirectory = "", int waitForExit = 2147483647, int bufferSize = 4096)

Parameters

exePath string

The path to the executable file to run.

arguments string

Command line arguments to pass into the executable.

outputWriter TextWriter

The text writer to forward the standard output stream to.

errorWriter TextWriter

The text writer to forward the standard error stream to.

workingDirectory string

The working directory to launch the executable in.

waitForExit int

A number of millisecoonds to wait for the process ending. Use MaxValue to wait infinitely.

bufferSize int

The sizes of the intermediate buffers to use for forwarding in number of char.

Returns

int

The exit code of the executable.

Examples

Launch an executable and forward its output and error streams while it is running.

var output = new StringWriter();
var error = new StringWriter();

var exitCode =  ExeHelper.Execute("dotnet.exe", "--UNKNOWN", output, error, string.Empty, 5000, 2048);

var o = output.ToString();
var e = error.ToString();

Launch an executable and forward its output and error streams to the Console of the main process.

var exitCode =  ExeHelper.Execute("dotnet.exe", "--UNKNOWN", Console.Out, Console.Error, string.Empty, 5000, 2048);

Exceptions

InvalidOperationException

See the inner exception for more details.

Execute(string, string, string)

Executes an executable by the specified exePath. Does not extract output and error streams.

public static int Execute(string exePath, string arguments, string workingDirectory = "")

Parameters

exePath string

The path to the executable file to run.

arguments string

Command line arguments to pass into the executable.

workingDirectory string

The working directory to launch the executable in.

Returns

int

The exit code of the executable.

Exceptions

InvalidOperationException

See the inner exception for more details.