A library that provides an easy way to execute a shell command and handle the output via delegates. It supports both synchronous and asynchronous execution with customizable output handling.
To install RunCommand, you can use the .NET CLI:
dotnet add package ktsu.RunCommand
Or you can use the NuGet Package Manager in Visual Studio to search for and install the ktsu.RunCommand package.
The simplest way to execute a command is to use the Execute
method. All methods return the process exit code:
using ktsu.RunCommand;
class Program
{
static void Main()
{
int exitCode = RunCommand.Execute("echo Hello World!");
if (exitCode == 0)
{
Console.WriteLine("Command executed successfully!");
}
else
{
Console.WriteLine($"Command failed with exit code: {exitCode}");
}
}
}
To handle the output of the command, you can provide delegates to the OutputHandler
class:
using ktsu.RunCommand;
class Program
{
static void Main()
{
int exitCode = RunCommand.Execute(
command: "echo Hello World!",
outputHandler: new(
onStandardOutput: Console.Write,
onStandardError: Console.Write
)
);
Console.WriteLine($"Process exited with code: {exitCode}");
}
}
NOTE: When using the default OutputHandler, the delegates will receive undelimited chunks of output. This gives you the flexibility to receive exactly the output the command produces, including whitespace and non-printable characters, and handle it as you see fit.
If you prefer to handle the output line by line, you can use the LineOutputHandler
class:
using ktsu.RunCommand;
class Program
{
static void Main()
{
int exitCode = RunCommand.Execute(
command: "echo Hello World!",
outputHandler: new LineOutputHandler(
onStandardOutput: line => Console.WriteLine($"Output: {line}"),
onStandardError: line => Console.WriteLine($"Error: {line}")
)
);
Console.WriteLine($"Process exited with code: {exitCode}");
}
}
All of the above examples can be executed asynchronously by using the ExecuteAsync
method:
using ktsu.RunCommand;
class Program
{
static async Task Main()
{
int exitCode = await RunCommand.ExecuteAsync("echo Hello World!");
if (exitCode == 0)
{
Console.WriteLine("Command executed successfully!");
}
else
{
Console.WriteLine($"Command failed with exit code: {exitCode}");
}
}
}
By default, the library uses the UTF-8 encoding for the input and output streams. If you need to use a different encoding, you can specify it in the OutputHandler
or LineOutputHandler
constructor:
using System.Text;
using ktsu.RunCommand;
class Program
{
static void Main()
{
int exitCode = RunCommand.Execute(
command: "echo Hello World!",
outputHandler: new(
onStandardOutput: Console.Write,
onStandardError: Console.Write,
encoding: Encoding.ASCII
)
);
}
}
-
Execute(string command)
: Executes a command synchronously and returns the process exit code. -
Execute(string command, OutputHandler outputHandler)
: Executes a command synchronously with custom output handling and returns the process exit code. -
ExecuteAsync(string command)
: Executes a command asynchronously and returns a task with the process exit code. -
ExecuteAsync(string command, OutputHandler outputHandler)
: Executes a command asynchronously with custom output handling and returns a task with the process exit code.
Processes output in raw chunks:
OutputHandler(onStandardOutput, onStandardError)
: Constructor with handlers for output and error streams.
Processes output line by line:
LineOutputHandler(onStandardOutput, onStandardError)
: Constructor with handlers for output and error streams.
NOTE: The
OutputHandler
classes receive undelimited chunks of output directly from the process stream. TheLineOutputHandler
buffers this output and splits it by newline characters, invoking the delegates for each complete line.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
Thanks to the .NET community and ktsu.dev contributors for their support.