/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ namespace NRKernal { using System.Diagnostics; using System.Text; /// Misc helper methods for running shell commands. public static class ShellHelper { /// Run a shell command. /// File name for the executable. /// Command line arguments, space delimited. /// [out] Filled out with the result as printed to stdout. /// [out] Filled out with the result as printed to stderr. public static void RunCommand(string fileName, string arguments, out string output, out string error) { using (var process = new Process()) { var startInfo = new ProcessStartInfo(fileName, arguments); startInfo.UseShellExecute = false; startInfo.RedirectStandardError = true; startInfo.RedirectStandardOutput = true; startInfo.CreateNoWindow = true; process.StartInfo = startInfo; var errorBuilder = new StringBuilder(); process.ErrorDataReceived += (sender, ef) => errorBuilder.AppendLine(ef.Data); process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); var existcode = process.ExitCode; process.Close(); // Trims the output strings to make comparison easier. output = existcode.ToString(); error = errorBuilder.ToString().Trim(); } } /// Run a shell command. /// File name for the executable. /// Command line arguments, space delimited. public static void RunCommand(string fileName, string arguments) { using (var process = new Process()) { var startInfo = new ProcessStartInfo(fileName, arguments); startInfo.CreateNoWindow = false; process.StartInfo = startInfo; process.Start(); } } } }