Introduction to SimpleExec
SimpleExec is a versatile and user-friendly .NET library designed to simplify the execution of external commands within software applications. Built to harness the power of the System.Diagnostics.Process
class, SimpleExec provides a straightforward and effective approach to running commands without the need for engaging the system shell. This feature makes it particularly appealing for developers working within the .NET 6.0 framework and beyond.
Key Features
- No System Shell Invocation: SimpleExec stands out by not using the system shell, thereby reducing complexity and potential security issues.
- Compatibility: Supports .NET 6.0 and later, ensuring usage with modern applications.
- Command Execution: Allows running commands with ease through its intuitive interface.
- Error Handling: Provides mechanisms for handling command exit codes, making it easier to manage different command outcomes.
- Customization Options: Offers various optional arguments for customization according to specific developer needs.
Quick Start
Getting started with SimpleExec is as easy as including a simple namespace and running a command. Developers can use the following code snippet to familiarize themselves with the library's basic usage:
using static SimpleExec.Command;
Run("foo", "arg1 arg2");
This snippet demonstrates how effortless it is to invoke external commands with SimpleExec.
Running Commands
SimpleExec provides multiple options for running commands synchronously or asynchronously, making it flexible for different application needs:
Run("foo");
Run("foo", "arg1 arg2");
Run("foo", new[] { "arg1", "arg2" });
await RunAsync("foo");
await RunAsync("foo", "arg1 arg2");
await RunAsync("foo", new[] { "arg1", "arg2" });
For transparency, executed commands are echoed to the standard output by default.
Reading Command Output
Beyond just running commands, SimpleExec allows developers to read the standard output and error streams effectively:
var (standardOutput, standardError) = await ReadAsync("foo");
This feature is useful for capturing and utilizing the command's output programmatically.
Other Optional Arguments
SimpleExec comes with a suite of optional arguments such as specifying a working directory, configuring the environment, and managing window creation, among others:
string workingDirectory = "",
bool noEcho = false,
string? echoPrefix = null,
Action<IDictionary<string, string?>>? configureEnvironment = null,
bool createNoWindow = false,
Encoding? encoding = null,
Func<int, bool>? handleExitCode = null,
string? standardInput = null,
bool cancellationIgnoresProcessTree = false,
CancellationToken cancellationToken = default,
These options empower developers to tailor the command execution process to their specific requirements.
Handling Exceptions
SimpleExec ensures robust error handling by throwing exceptions when commands exit with non-zero codes. These exceptions provide detailed information about the exit code and any accompanying output or errors:
$"The process exited with code {ExitCode}."
Developers can customize exit code handling to suppress exceptions for known non-error codes, such as with Robocopy, which might indicate success with a non-zero exit code.
Conclusion
SimpleExec is a powerful tool for developers needing a safe and efficient way to run external commands from within their .NET applications. Its straightforward syntax, comprehensive error handling, and adaptable customization options make it an indispensable asset in any .NET developer's toolkit.