Advertisement
LaughingMan

Untitled

Aug 20th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. public static bool RunCommand(FileInfo command, IReadOnlyCollection<string> args, RunCommandOptions options = null)
  2.         {
  3.             try
  4.             {
  5.                 if (command == null
  6.                     || !command.Exists)
  7.                     throw new FileNotFoundException("The file specified does not exist.");
  8.  
  9.                 Console.Write($"\rExecuting Command: {command.Name}\t\t\t\t\t");
  10.  
  11.                 options = options ?? new RunCommandOptions();
  12.  
  13.                 string commandPath = command.FullName.Contains(" ")
  14.                     ? $"\"{command.FullName}\""
  15.                     : command.FullName;
  16.  
  17.                 Console.Write("\rInstantiating ProcessStartInfo Object\t\t\t\t\t");
  18.                 ProcessStartInfo psi = new ProcessStartInfo(command.FullName)
  19.                 {
  20.                     CreateNoWindow = options.CreateNoWindow,
  21.                     UseShellExecute = options.UseShellExecute,
  22.                     RedirectStandardError = options.RedirectStandardError,
  23.                     RedirectStandardInput = options.RedirectStandardInput,
  24.                     RedirectStandardOutput = options.RedirectStandardOutput,
  25.                     WindowStyle = options.WindowStyle,
  26.                     Arguments = string.Join(" ", args),
  27.                     FileName = commandPath,
  28.                     WorkingDirectory = OutputPath
  29.                 };
  30.  
  31.                 //Debug.WriteLine($"Command: {psi.FileName} {psi.Arguments}");
  32.  
  33.                 Console.Write("\rBeginning Process Execution\t\t\t\t\t");
  34.                 using (Process process = new Process())
  35.                 {
  36.                     process.ErrorDataReceived += Process_ErrorDataReceived;
  37.                     process.OutputDataReceived += Process_OutputDataReceived;
  38.                     process.StartInfo = psi;
  39.  
  40.                     bool startResult = process.Start();
  41.  
  42.                     StreamReader output = process.StandardOutput;
  43.                     StreamReader error = process.StandardError;
  44.  
  45.                     Console.Write($"\rCommand process has started: {process.StartTime}\t\t\t\t\t");
  46.                     if (startResult)
  47.                     {
  48.                         //StreamReader output = process.StandardOutput;
  49.                         process.WaitForExit();
  50.  
  51.                         //Console.WriteLine(output.ReadToEnd());
  52.                         Console.Write($"\rProcess has exited: Code {process.ExitCode} | {process.ExitTime}\t\t\t\t\t");
  53.                     }
  54.  
  55.                     string outputDirectory = Path.Combine(OutputPath, "Logs");
  56.                     string errorPath = Path.Combine(outputDirectory, "ProcessErrorLog.txt");
  57.                     string outputPath = Path.Combine(outputDirectory, "ProcessOutputLog.txt");
  58.  
  59.                     if (process.ExitCode != 0)
  60.                     {
  61.                         using (
  62.                             FileStream fs = new FileStream(errorPath,
  63.                                 FileMode.Append,
  64.                                 FileSystemRights.AppendData,
  65.                                 FileShare.ReadWrite,
  66.                                 4096,
  67.                                 FileOptions.Asynchronous))
  68.                         {
  69.                             string errorString = error.ReadToEnd();
  70.  
  71.                             fs.WriteAsync(Encoding.UTF8.GetBytes(errorString), 0, errorString.Length);      
  72.                         }
  73.                     }
  74.  
  75.                     using (
  76.                         FileStream fs = new FileStream(outputPath,
  77.                             FileMode.Append,
  78.                             FileSystemRights.AppendData,
  79.                             FileShare.ReadWrite,
  80.                             4096,
  81.                             FileOptions.Asynchronous))
  82.                     {
  83.                         string outputString = output.ReadToEnd();
  84.                         fs.WriteAsync(Encoding.UTF8.GetBytes(outputString), 0, outputString.Length);
  85.                     }
  86.  
  87.                     bool hasTerminated = false;
  88.  
  89.                     Stopwatch sw = Stopwatch.StartNew();
  90.  
  91.                     while (!hasTerminated)
  92.                     {
  93.                         hasTerminated = process.HasExited;
  94.                         if (sw.Elapsed.Seconds >= 10)
  95.                             break;
  96.                     }
  97.                     sw.Stop();
  98.  
  99.                     Console.Write("\rEnding Process Execution\t\t\t\t\t");
  100.                     return true;
  101.                     //return process.ExitCode == 0;
  102.                 }
  103.             }
  104.             catch (Exception e)
  105.             {
  106.                 Console.WriteLine($"\r\n{e.Message}\r\n{e}");
  107.                 throw;
  108.             }
  109.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement