andrew4582

GetExecutableOutput

Dec 3rd, 2010
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1.         public static string GetExecutableOutput(string sExecute, string sParams, out int iExitCode)
  2.         {
  3.             iExitCode = -999;
  4.             StringBuilder builder = new StringBuilder();
  5.             builder.Append("Results from " + sExecute + " " + sParams + "\r\n\r\n");
  6.             try
  7.             {
  8.                 string str;
  9.                 Process process = new Process {
  10.                     StartInfo = { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = false, CreateNoWindow = true, FileName = sExecute, Arguments = sParams }
  11.                 };
  12.                 process.Start();
  13.                 while ((str = process.StandardOutput.ReadLine()) != null)
  14.                 {
  15.                     str = str.TrimEnd(new char[0]);
  16.                     if (str != string.Empty)
  17.                     {
  18.                         builder.Append(str + "\r\n");
  19.                     }
  20.                 }
  21.                 iExitCode = process.ExitCode;
  22.                 process.Dispose();
  23.             }
  24.             catch (Exception exception)
  25.             {
  26.                 builder.Append("Exception thrown: " + exception.ToString() + "\r\n" + exception.StackTrace.ToString());
  27.             }
  28.             builder.Append("-------------------------------------------\r\n");
  29.             return builder.ToString();
  30.         }
Add Comment
Please, Sign In to add comment