Пример #1
0
        /// <summary>
        /// Starts to run a command as external process with 'cmd.exe' (Windows) or '/bin/bash' (Linux/macOS)
        /// </summary>
        /// <param name="command">The command to run</param>
        /// <param name="workingDirectory">The working directory</param>
        /// <param name="onExited">The action to run when the process was exited (Exited event)</param>
        /// <param name="onOutputDataReceived">The action to run when an output message was received (OutputDataReceived event)</param>
        /// <param name="onErrorDataReceived">The action to run when an error message was received (ErrorDataReceived event)</param>
        /// <param name="captureOutput">true to capture output (standard output and error output)</param>
        /// <returns></returns>
        /// <remarks>
        /// Remember assign execution permisions to the file (sudo chmod 777 'filename') while running on Linux/macOS
        /// </remarks>
        public static Info Start(string command, string workingDirectory = null, Action <object, EventArgs> onExited = null, Action <object, DataReceivedEventArgs> onOutputDataReceived = null, Action <object, DataReceivedEventArgs> onErrorDataReceived = null, bool captureOutput = false)
        {
            var arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                                ? $"/c \"{command.Replace("\"", "\"\"\"")}\""
                                : $"-c \"{command.Replace("\"", "\\\"")}\"";

            return(ExternalProcess.Start(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/bash", arguments, workingDirectory, onExited, onOutputDataReceived, onErrorDataReceived, captureOutput));
        }
Пример #2
0
 /// <summary>
 /// Starts to run an external process directly
 /// </summary>
 /// <param name="filePath">The absolute path to the file of external process</param>
 /// <param name="arguments">The arguments</param>
 /// <param name="onExited">The action to run when the process was exited (Exited event)</param>
 /// <param name="onDataReceived">The method to handle the data receive events (include OutputDataReceived and ErrorDataReceived events)</param>
 /// <returns></returns>
 /// <remarks>
 /// Remember assign execution permisions to the file (sudo chmod 777 'filename') while running on Linux/macOS
 /// </remarks>
 public static Info Start(string filePath, string arguments, Action <object, EventArgs> onExited, Action <object, DataReceivedEventArgs> onDataReceived = null)
 => ExternalProcess.Start(filePath, arguments, null, onExited, onDataReceived, onDataReceived, false);