Command Helper 类
提供在 Windows 命令提示符(cmd.exe)下执行命令的方法。
Definition
命名空间: Suresking.Utility
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
C#
public static class CommandHelper- Inheritance
- Object CommandHelper
备注
使用示例:
// 执行命令,不获取输出(同步)
CommandHelper.Execute("mkdir test");
// 执行命令,不获取输出(异步)
await CommandHelper.ExecuteAsync("mkdir test");
// 同步执行并获取输出
var result = CommandHelper.ExecuteWithOutput("dir");
Console.WriteLine($"退出码: {result.ExitCode}");
Console.WriteLine($"输出: {result.Output}");
// 异步执行并获取输出
var result = await CommandHelper.ExecuteWithOutputAsync("dir");
Console.WriteLine($"退出码: {result.ExitCode}");
Console.WriteLine($"输出: {result.Output}");
// 异步 + 超时取消
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var result = await CommandHelper.ExecuteWithOutputAsync(
"dotnet format . --verify-no-changes",
workingDirectory: @"D:\Projects\MyProject",
cancellationToken: cts.Token);
// 检查 dotnet format 的特定退出码
if (result.ExitCode == 2)
{
Console.WriteLine("代码需要格式化");
}
方法
| Execute | 执行命令,不等待完成、不获取输出(同步)。 |
| ExecuteAndWait | 执行命令,等待完成但不获取输出(同步)。 |
| ExecuteAndWaitAsync | 执行命令,等待完成但不获取输出(异步)。 |
| ExecuteDirectAsync | 执行命令并实时报告进度(异步)- 直接调用可执行文件。 |
| ExecuteWithOutput | 执行命令并获取输出(同步)。 |
| ExecuteWithOutputAsync | 执行命令并获取输出(异步)。 |
| ExecuteWithProgressAsync | 执行命令并实时报告进度(异步)。 |