|
| 1 | +# CommandsExecutor |
| 2 | + |
| 3 | +This is a class to help to run commands in system shell. |
| 4 | + |
| 5 | +It provides 2 static methods with each have a extension method. |
| 6 | + |
| 7 | +## Static Methods |
| 8 | + |
| 9 | +### Methods and arguments with description |
| 10 | + |
| 11 | +- `GetExecutionResult` -> `string` |
| 12 | + |
| 13 | + This method execute command in system shell, and return standard output content. |
| 14 | + |
| 15 | + `string` **command** - command name, when **findInPath** is true, file path will find from `Path`. |
| 16 | + |
| 17 | + `string` **args** - start up arguments. |
| 18 | + |
| 19 | + `[bool]` **findInPath** = false - indecate whether get file path in `Path`. |
| 20 | + |
| 21 | + `[Action<ProcessStartInfo>?]` **action** = null - user can user this argument to operate `ProcessStartInfo`. |
| 22 | + |
| 23 | +- `GetExecutionResultAsync` -> `Task<string>` |
| 24 | + |
| 25 | + This method has same function and 4 same parameters as `GetExecutionResult`, but execute outside program asynchronously, |
| 26 | +you can await this method. |
| 27 | + |
| 28 | + `string`, `string`, `[bool]`, `[Action<ProcessStartInfo>?]` |
| 29 | + |
| 30 | + `[CancellationToken?]` **token** = null - you can use this object to cancel this task, once **token**!.`IsCancellationRequested` is true, the outside process will be killed. |
| 31 | + |
| 32 | +### Usage |
| 33 | + |
| 34 | +```csharp |
| 35 | +using Common.BasicHelper.Core.Shell; |
| 36 | + |
| 37 | +var output = CommandsExecutor.GetExecutionResult( |
| 38 | + "help", // command, use `help` for example |
| 39 | + "", |
| 40 | + true |
| 41 | +); |
| 42 | + |
| 43 | +var output_async - CommandsExecutor.GetExecutionResultAsync( |
| 44 | + "help", |
| 45 | + "", |
| 46 | + true |
| 47 | +); |
| 48 | + |
| 49 | +Console.WriteLine(output); |
| 50 | +Console.WriteLine(output_async); |
| 51 | +``` |
| 52 | + |
| 53 | +## Extension Methods |
| 54 | + |
| 55 | +### Methods and arguments with description |
| 56 | + |
| 57 | +- `ExecuteAsCommand` -> `string` |
| 58 | + |
| 59 | + This extension method is a wrapping method for `GetExecutionResult`. |
| 60 | + |
| 61 | + *this* `string` **command** |
| 62 | + |
| 63 | + `[string?]` **args** = null |
| 64 | + |
| 65 | + `[bool]` **findInPath** = true |
| 66 | + |
| 67 | + `[Action<ProcessStartInfo>?]` **action** = null |
| 68 | + |
| 69 | +- `ExecuteAsCommandAsync` -> `Task<string>` |
| 70 | + |
| 71 | + This extension method is a wrapping method for `GetExecutionResultAsync`. |
| 72 | + |
| 73 | + *this* `string` **command** |
| 74 | + |
| 75 | + `[string?]` **args** = null |
| 76 | + |
| 77 | + `[bool]` **findInPath** = true |
| 78 | + |
| 79 | + `[Action<ProcessStartInfo>?]` **action** = null |
| 80 | + |
| 81 | + `[CancellationToken?]` **token** = default |
| 82 | + |
| 83 | +### Usage |
| 84 | + |
| 85 | +```csharp |
| 86 | +using Common.BasicHelper.Core.Shell; |
| 87 | + |
| 88 | +Console.WriteLine("help".ExecuteAsCommand()); |
| 89 | +``` |
| 90 | + |
| 91 | +## OOP |
| 92 | + |
| 93 | +This is a static class. |
| 94 | + |
| 95 | +## Examples |
| 96 | + |
| 97 | +- In [KitX Dashboard](https://github.com/Crequency/KitX-Dashboard/blob/03e4d3b127de69a34df00f6ac34db7ec6bd1a0d4/Network/NetworkHelper.cs#L139) project, we use these functions to get system version info. |
| 98 | + |
0 commit comments