/// <summary> /// Run a program in the output window. /// </summary> /// <param name="exe">The path of the program to run.</param> /// <param name="args">Arguments to be passed to the program.</param> /// <param name="clearTitle">Clear the output window title bar if true.</param> /// <returns>True if the program returns 0 on exit.</returns> public bool RunProgram(string exe, string args, bool clearTitle) { /* * Reset the window title bar. */ if (clearTitle) { Text = Resources.OutputWindowTitle; } /* * Run the process. */ RunProcessContext context = new RunProcessContext(); context.ExePath = exe; context.ProcessArgs = args; context.HeaderText = String.Format("{0}: ", Resources.RunProgramCaption); RunProcessInternal(context); return(context.ExitCode == 0); }
/// <summary> /// Run a shell command and redirect standard output and error /// streams to the output views. /// </summary> /// <param name="caption">Text to display before the output of the command.</param> /// <param name="cmd">The command to run.</param> /// <param name="clearTitle">Clear the output window title bar if true.</param> /// <returns>True if the command returns 0 on exit.</returns> public bool RunShellCommandInternal( string caption, string cmd, bool clearTitle) { /* * Reset the window title bar. */ if (clearTitle) { Text = Resources.OutputWindowTitle; } /* * Do some simple macro substitutions. */ ApplicationManager applicationManager = ApplicationManager.GetInstance(); string qshome = applicationManager.QuickSharpHome; string qsuser = applicationManager.QuickSharpUserHome; string system = Environment.SystemDirectory; string pfiles = Environment.GetFolderPath( Environment.SpecialFolder.ProgramFiles); string mydocs = Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments); cmd = cmd.Replace("${IDE_HOME}", qshome); cmd = cmd.Replace("${USR_HOME}", qsuser); cmd = cmd.Replace("${USR_DOCS}", mydocs); cmd = cmd.Replace("${SYSTEM}", system); cmd = cmd.Replace("${PFILES}", pfiles); /* * Run the command. */ RunProcessContext context = new RunProcessContext(); String comspec = Environment.GetEnvironmentVariable( Constants.WINDOWS_COMSPEC); if (String.IsNullOrEmpty(comspec)) { comspec = Path.Combine( Environment.SystemDirectory, Constants.WINDOWS_SHELL_PROCESSOR); } context.ExePath = comspec; context.ProcessArgs = "/D /C " + cmd; context.HeaderText = caption; RunProcessInternal(context); return(context.ExitCode == 0); }
/// <summary> /// Run a new process in a separate window. /// </summary> /// <param name="context">The process context information.</param> /// <returns>True if the process exits without error.</returns> public bool RunProcessExternal(RunProcessContext context) { ProcessStartInfo pi = CreateProcessStartInfo(context, false); bool result = false; _listViewListView.Columns[0].Width = _listViewListView.Width - 25; // Header string header = String.Format("------ {0}{1} {2}", context.HeaderText, context.ExePath, context.ProcessArgs); AddLineToOutputView(header); // Run process try { Process p = Process.Start(pi); while (!p.HasExited) { Thread.Sleep(10); mainForm.Update(); } context.ExitCode = p.ExitCode; result = true; } catch (Exception ex) { MessageBox.Show(String.Format("{0}\r\n{1}", Resources.RunProcessErrorMessage, ex.Message), Resources.RunProcessErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } // Footer string footer = String.Format("------ {0}{1} {2}", context.FooterText, Resources.RunProcessReturn, context.ExitCode); AddLineToOutputView(footer); AdjustOutputWidth(); MoveToEndOfOutput(); return(result); }
private ProcessStartInfo CreateProcessStartInfo( RunProcessContext context, bool runInternal) { ProcessStartInfo pi = new ProcessStartInfo(); pi.FileName = String.Format("\"{0}\"", context.ExePath); pi.Arguments = context.ProcessArgs; pi.UseShellExecute = false; if (runInternal) { pi.CreateNoWindow = true; pi.RedirectStandardOutput = true; pi.RedirectStandardError = true; } else { pi.CreateNoWindow = false; pi.RedirectStandardOutput = false; pi.RedirectStandardError = false; } return(pi); }
/// <summary> /// Run the process and present the standard and error output /// streams in the output window views. /// </summary> /// <param name="context">The process context information.</param> /// <returns>True if the process exits without error.</returns> public bool RunProcessInternal(RunProcessContext context) { bool result = true; context.Output = this; try { ProcessStartInfo pi = CreateProcessStartInfo(context, true); mainForm.Cursor = Cursors.WaitCursor; _listViewListView.Columns[0].Width = _listViewListView.Width - 25; // Header string header = String.Format("------ {0}{1} {2}", context.HeaderText, context.ExePath, context.ProcessArgs); AddLineToOutputView(header); // Run process try { Process p = Process.Start(pi); p.OutputDataReceived += new DataReceivedEventHandler( context.OutputDataReceived); p.ErrorDataReceived += new DataReceivedEventHandler( context.ErrorDataReceived); p.BeginOutputReadLine(); p.BeginErrorReadLine(); while (!p.HasExited) { Thread.Sleep(10); mainForm.Update(); } p.WaitForExit(); context.ExitCode = p.ExitCode; } catch (Exception ex) { AddLineToOutputView(ex.Message); result = false; } // Footer string footer = String.Format("------ {0}{1} {2}", context.FooterText, Resources.RunProcessReturn, context.ExitCode); AddLineToOutputView(footer); MoveToEndOfOutput(); AdjustOutputWidth(); } finally { mainForm.Cursor = Cursors.Default; } return(result); }