Exemple #1
0
        public ProcessReturn Run(ProcessStartInfo info, TimeSpan waitDuration, Action <string> callback)
        {
            //use the operating system shell to start the process
            //this allows credentials to flow through.
            //info.UseShellExecute = true;
            info.UseShellExecute = false;
            info.Verb            = "runas";
            info.WindowStyle     = ProcessWindowStyle.Normal;

            //don't open a new terminal window
            info.CreateNoWindow = true;

            info.RedirectStandardError = info.RedirectStandardOutput = true;

            //if (!Path.IsPathRooted(info.FileName))
            //{B
            //    info.FileName = info.WorkingDirectory.AppendPath(info.FileName);
            //}

            ProcessReturn returnValue = null;
            var           output      = new StringBuilder();
            int           pid         = 0;

            using (var proc = Process.Start(info))
            {
                pid = proc.Id;
                proc.OutputDataReceived += (sender, outputLine) =>
                {
                    if (outputLine.Data.IsNotEmpty())
                    {
                        callback(outputLine.Data);
                    }
                    output.AppendLine(outputLine.Data);
                };

                proc.BeginOutputReadLine();
                proc.WaitForExit((int)waitDuration.TotalMilliseconds);

                killProcessIfItStillExists(pid);

                returnValue = new ProcessReturn()
                {
                    ExitCode   = proc.ExitCode,
                    OutputText = output.ToString()
                };
            }

            return(returnValue);
        }
        public ProcessReturn Run(ProcessStartInfo info, TimeSpan waitDuration, Action<string> callback)
        {
            //use the operating system shell to start the process
            //this allows credentials to flow through.
            //info.UseShellExecute = true;
            info.UseShellExecute = false;
            info.Verb = "runas";
            info.WindowStyle = ProcessWindowStyle.Normal;

            //don't open a new terminal window
            info.CreateNoWindow = true;

            info.RedirectStandardError = info.RedirectStandardOutput = true;

            //if (!Path.IsPathRooted(info.FileName))
            //{B
            //    info.FileName = info.WorkingDirectory.AppendPath(info.FileName);
            //}

            ProcessReturn returnValue = null;
            var output = new StringBuilder();
            int pid = 0;
            using (var proc = Process.Start(info))
            {
                pid = proc.Id;
                proc.OutputDataReceived += (sender, outputLine) =>
                {
                    if (outputLine.Data.IsNotEmpty())
                    {
                        callback(outputLine.Data);
                    }
                    output.AppendLine(outputLine.Data);
                };

                proc.BeginOutputReadLine();
                proc.WaitForExit((int)waitDuration.TotalMilliseconds);

                killProcessIfItStillExists(pid);

                returnValue = new ProcessReturn()
                              {
                                  ExitCode = proc.ExitCode,
                                  OutputText = output.ToString()
                              };
            }

            return returnValue;
        }