public static ExecuteResult Execute(string aUserName, System.Security.SecureString aCheck, string aExe, string aArgs, System.Diagnostics.ProcessPriorityClass aPriority, string aStdin, ExecOutputDelegate aOutput, ExecOutputDelegate aError) { string Domain = string.Empty; string User = ParseUserName(aUserName, out Domain); bool HasStdin = !string.IsNullOrEmpty(aStdin); ExecuteResult Result = null; try { using (System.Diagnostics.Process P = new System.Diagnostics.Process()) { P.StartInfo = new System.Diagnostics.ProcessStartInfo(aExe, aArgs) { UseShellExecute = false, RedirectStandardInput = HasStdin, RedirectStandardError = true, RedirectStandardOutput = true, CreateNoWindow = true, UserName = User, Domain = Domain, Password = aCheck, LoadUserProfile = true, }; // These will read while the process is running. StdHandler Out = new StdHandler(OutputSource.stdout, P, aOutput); StdHandler Err = new StdHandler(OutputSource.stderr, P, aError); // Go P.Start(); // Start the handlers Out.Start(); Err.Start(); // Send any imput to the process if (HasStdin) { P.StandardInput.WriteLine(aStdin); P.StandardInput.Flush(); } // Gotta set priority AFTER start P.PriorityClass = aPriority; // And wait till it is done (timeout?) P.WaitForExit(); // And done, build a result thingy Result = new ExecuteResult(P, Out, Err); } } catch (Exception Ex) { Debug.Write("Execute (" + aExe + "): "); Debug.WriteLine(Ex); Result = new ExecuteResult(Ex); } return Result; }
public ExecuteResult(System.Diagnostics.Process aProcess, StdHandler aOut, StdHandler aErr) { Ex = null; Error = false; Stdout = aOut.Standard; Stderr = aErr.Standard; ExitCode = aProcess.ExitCode; }