| | | 1 | | using System.Diagnostics; |
| | | 2 | | |
| | | 3 | | namespace pva.Helpers |
| | | 4 | | { |
| | | 5 | | public static class SystemCommand |
| | | 6 | | { |
| | | 7 | | public static void Run(string command, string args, out string output, out string error, string? directory = nul |
| | 36 | 8 | | { |
| | 36 | 9 | | string actualCommand = String.Empty; |
| | 36 | 10 | | string arguments = command; |
| | 36 | 11 | | if (OperatingSystem.IsWindows()) |
| | 0 | 12 | | { |
| | 0 | 13 | | actualCommand = "cmd.exe"; |
| | 0 | 14 | | arguments = $"/c {command} {args}"; |
| | 0 | 15 | | } |
| | 36 | 16 | | else if (OperatingSystem.IsLinux()) |
| | 36 | 17 | | { |
| | 36 | 18 | | actualCommand = command; |
| | 36 | 19 | | arguments = args; |
| | 36 | 20 | | } |
| | 36 | 21 | | using Process process = new() |
| | 36 | 22 | | { |
| | 36 | 23 | | StartInfo = new ProcessStartInfo |
| | 36 | 24 | | { |
| | 36 | 25 | | FileName = actualCommand, |
| | 36 | 26 | | UseShellExecute = false, |
| | 36 | 27 | | RedirectStandardOutput = true, |
| | 36 | 28 | | RedirectStandardError = true, |
| | 36 | 29 | | RedirectStandardInput = true, |
| | 36 | 30 | | Arguments = arguments, |
| | 36 | 31 | | CreateNoWindow = true, |
| | 36 | 32 | | WorkingDirectory = directory ?? string.Empty, |
| | 36 | 33 | | } |
| | 36 | 34 | | }; |
| | 36 | 35 | | process.Start(); |
| | 36 | 36 | | process.WaitForExit(); |
| | 36 | 37 | | output = process.StandardOutput.ReadToEnd(); |
| | 36 | 38 | | error = process.StandardError.ReadToEnd(); |
| | 72 | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | } |