Beispiel #1
0
        /// <summary>
        /// Execute process and return result in the form of ProcessResult.
        /// </summary>
        /// <remarks>No exception is expected from this application. Execution error will be reflected in ExitCode.</remarks>
        /// <param name="waitForExit">Set to true to wait until process exit, else return once process started.</param>
        /// <returns>Execution result as <see cref="ProcessResult"/></returns>
        public ProcessResult Execute(bool waitForExit = true)
        {
            ProcessResult result = new ProcessResult();

            outputData    = new List <string>();
            errorDetected = false;
            try
            {
                processInfo          = new ProcessStartInfo();
                processInfo.FileName = Application;
                if (Application.Contains(" "))
                {
                    processInfo.FileName = "\"" + processInfo.FileName + "\"";
                }
                processInfo.WorkingDirectory       = WorkingDirectory;
                processInfo.Arguments              = Arguments;
                processInfo.CreateNoWindow         = !ShowConsoleWindow;
                processInfo.UseShellExecute        = ShowConsoleWindow;
                processInfo.RedirectStandardOutput = !ShowConsoleWindow;
                processInfo.RedirectStandardError  = !ShowConsoleWindow;
                if (!string.IsNullOrEmpty(UserName))
                {
                    processInfo.UserName = UserName;
                    processInfo.Password = ssPwd;
                    processInfo.Domain   = DomainName;
                }
                Trace.WriteLine(Name + ": Executing: " + processInfo.FileName + " " + processInfo.Arguments);
                if (ProcessHandler != null)
                {
                    DisposeProcessHandler();
                }
                ProcessHandler = Process.Start(processInfo);
                if (!ShowConsoleWindow)
                {
                    ProcessHandler.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
                    ProcessHandler.ErrorDataReceived  += new DataReceivedEventHandler(process_ErrorDataReceived);
                    ProcessHandler.BeginOutputReadLine();
                    ProcessHandler.BeginErrorReadLine();
                }
                if (!waitForExit)
                {
                    ProcessHandler.EnableRaisingEvents = true;
                    if (ProcessHandler.EnableRaisingEvents)
                    {
                        ProcessHandler.Exited += new EventHandler(process_Exited);
                    }
                    return(null);
                }

                ProcessHandler.WaitForExit();
                result.ExitCode = ProcessHandler.ExitCode;
            }
            catch (Exception ex)
            {
                result.Output        = new string[] { ex.ToString() };
                result.ErrorDetected = true;
                result.ExitCode      = -999;
                return(result);
            }
            ProcessHandler.OutputDataReceived -= process_OutputDataReceived;
            ProcessHandler.ErrorDataReceived  -= process_ErrorDataReceived;

            result.ErrorDetected = errorDetected;
            result.Output        = outputData.ToArray();
            return(result);
        }