public ProcessRunner() { os = new Os(); startInfo = new ProcessStartInfo { FileName = os.IsLinux() ? "/bin/bash" : "pwsh.exe", RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, UseShellExecute = false }; log = new Logger(GetType().Name); }
public string Run(string command) { output = null; lastCommand = command; string escaped = command.Replace("\"", "\\\""); using (System.Diagnostics.Process process = new System.Diagnostics.Process()) { process.StartInfo = startInfo; process.StartInfo.Arguments = os.IsLinux() ? $"-c \"{escaped}\"" : $"/C {command}"; process.Start(); process.WaitForExit(); output = process.StandardOutput.ReadToEnd(); error = process.StandardError.ReadToEnd(); } if (error.Length > 0) { throw new CustomProcessException(error); } return(output); }
static void Main(string[] args) { Console.WriteLine("Execute system command:"); if (Os.IsWindows()) { Os.SystemCommand("echo You are on Windows"); } else if (Os.IsLinux()) { Os.SystemCommand("echo You are on Linux"); } Os.SystemCommand("git --version"); Console.ReadKey(); }