Exemplo n.º 1
0
        /// <summary>
        /// Launches a program
        /// </summary>
        /// <param name="ProgramFile">Path to the program to run</param>
        /// <param name="Arguments">Command-line arguments</param>
        /// <param name="OnExit">Optional callback whent he program exits</param>
        /// <param name="OutputHandler">If supplied, std-out and std-error will be redirected to this function and no shell window will be created</param>
        /// <returns>The newly-created process, or null if it wasn't able to start.  Exceptions are swallowed, but a debug output string is emitted on errors</returns>
        public System.Diagnostics.Process LaunchProgram(string ProgramFile, string Arguments, EventHandler OnExit = null, DataReceivedEventHandler OutputHandler = null, bool bWaitForCompletion = false)
        {
            // Create the action's process.
            ProcessStartInfo ActionStartInfo = new ProcessStartInfo();

            ActionStartInfo.FileName  = ProgramFile;
            ActionStartInfo.Arguments = Arguments;

            if (OutputHandler != null)
            {
                ActionStartInfo.RedirectStandardInput  = true;
                ActionStartInfo.RedirectStandardOutput = true;
                ActionStartInfo.RedirectStandardError  = true;

                // True to use a DOS box to run the program in, otherwise false to run directly.  In order to redirect
                // output, UseShellExecute must be disabled
                ActionStartInfo.UseShellExecute = false;

                // Don't show the DOS box, since we're redirecting everything
                ActionStartInfo.CreateNoWindow = true;
            }


            Logging.WriteLine(String.Format("Executing: {0} {1}", ActionStartInfo.FileName, ActionStartInfo.Arguments));

            System.Diagnostics.Process ActionProcess;

            try
            {
                ActionProcess           = new System.Diagnostics.Process();
                ActionProcess.StartInfo = ActionStartInfo;

                if (OnExit != null)
                {
                    ActionProcess.EnableRaisingEvents = true;
                    ActionProcess.Exited += OnExit;
                }

                if (ActionStartInfo.RedirectStandardOutput)
                {
                    ActionProcess.EnableRaisingEvents = true;
                    ActionProcess.OutputDataReceived += OutputHandler;
                    ActionProcess.ErrorDataReceived  += OutputHandler;
                }

                // Launch the program
                ActionProcess.Start();

                if (ActionStartInfo.RedirectStandardOutput)
                {
                    ActionProcess.BeginOutputReadLine();
                    ActionProcess.BeginErrorReadLine();
                }

                if (bWaitForCompletion)
                {
                    while ((ActionProcess != null) && (!ActionProcess.HasExited))
                    {
                        ActionProcess.WaitForExit(50);
                    }
                }
            }
            catch (Exception Ex)
            {
                // Couldn't launch program
                Logging.WriteLine("Couldn't launch program: " + ActionStartInfo.FileName);
                Logging.WriteLine("Exception: " + Ex.Message);
                ActionProcess = null;
            }

            return(ActionProcess);
        }