ktsu.RunCommand
1.3.2
Prefix Reserved
dotnet add package ktsu.RunCommand --version 1.3.2
NuGet\Install-Package ktsu.RunCommand -Version 1.3.2
<PackageReference Include="ktsu.RunCommand" Version="1.3.2" />
<PackageVersion Include="ktsu.RunCommand" Version="1.3.2" />
<PackageReference Include="ktsu.RunCommand" />
paket add ktsu.RunCommand --version 1.3.2
#r "nuget: ktsu.RunCommand, 1.3.2"
#:package ktsu.RunCommand@1.3.2
#addin nuget:?package=ktsu.RunCommand&version=1.3.2
#tool nuget:?package=ktsu.RunCommand&version=1.3.2
ktsu.RunCommand
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.
Installation
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.
Usage
Basic Execution
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}");
}
}
}
Custom Output Handling
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.
Line-by-Line Output Handling
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}");
}
}
Asynchronous Execution
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}");
}
}
}
Encoding
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
)
);
}
}
API Reference
RunCommand Class
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.OutputHandler Class
Processes output in raw chunks:
OutputHandler(onStandardOutput, onStandardError)
: Constructor with handlers for output and error streams.
LineOutputHandler Class
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.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
Acknowledgements
Thanks to the .NET community and ktsu.dev contributors for their support.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
.NETStandard 2.1
- System.Memory (>= 4.6.3)
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.3.2 | 198 | 9/3/2025 |
1.3.2-pre.1 | 131 | 9/3/2025 |
1.3.1 | 298 | 6/5/2025 |
1.3.1-pre.2 | 64 | 4/26/2025 |
1.3.1-pre.1 | 120 | 4/4/2025 |
1.3.0 | 295 | 3/30/2025 |
1.2.0 | 102 | 3/29/2025 |
1.1.0 | 113 | 3/29/2025 |
0.1.0 | 103 | 3/29/2025 |
## v1.3.2 (patch)
Changes since v1.3.1:
- [patch] Force patch ([@matt-edmondson](https://github.com/matt-edmondson))
- [patch] Force patch ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.3.2-pre.1 (prerelease)
Changes since v1.3.1:
## v1.3.1 (patch)
Changes since v1.3.0:
- Remove Directory.Build.props, Directory.Build.targets, and several PowerShell scripts for metadata and version management. Add copyright headers to several source files in the RunCommand namespace. ([@matt-edmondson](https://github.com/matt-edmondson))
- Update DESCRIPTION.md to clarify shell command execution support and change project SDK references to ktsu.Sdk.Lib and ktsu.Sdk.Test version 1.8.0. ([@matt-edmondson](https://github.com/matt-edmondson))
- Update .editorconfig settings, enhance RunCommand functionality to return exit codes, and add SonarLint configuration ([@matt-edmondson](https://github.com/matt-edmondson))
- Add project configuration files and update SDK references ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.3.1-pre.2 (prerelease)
Changes since v1.3.1-pre.1:
- Sync .runsettings ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot]))
- Sync .editorconfig ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.3.1-pre.1 (prerelease)
Incremental prerelease update.
## v1.3.0 (minor)
Changes since v1.2.0:
- Add LICENSE template ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.2.0 (minor)
Changes since v1.1.0:
- [minor] Improve Git tag retrieval in changelog and version scripts ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance RunCommand library with output handling features ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.1.0 (major)
- Initial commit ([@matt-edmondson](https://github.com/matt-edmondson))