Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static bool RunCommand(FileInfo command, IReadOnlyCollection<string> args, RunCommandOptions options = null)
- {
- try
- {
- if (command == null
- || !command.Exists)
- throw new FileNotFoundException("The file specified does not exist.");
- Console.Write($"\rExecuting Command: {command.Name}\t\t\t\t\t");
- options = options ?? new RunCommandOptions();
- string commandPath = command.FullName.Contains(" ")
- ? $"\"{command.FullName}\""
- : command.FullName;
- Console.Write("\rInstantiating ProcessStartInfo Object\t\t\t\t\t");
- ProcessStartInfo psi = new ProcessStartInfo(command.FullName)
- {
- CreateNoWindow = options.CreateNoWindow,
- UseShellExecute = options.UseShellExecute,
- RedirectStandardError = options.RedirectStandardError,
- RedirectStandardInput = options.RedirectStandardInput,
- RedirectStandardOutput = options.RedirectStandardOutput,
- WindowStyle = options.WindowStyle,
- Arguments = string.Join(" ", args),
- FileName = commandPath,
- WorkingDirectory = OutputPath
- };
- //Debug.WriteLine($"Command: {psi.FileName} {psi.Arguments}");
- Console.Write("\rBeginning Process Execution\t\t\t\t\t");
- using (Process process = new Process())
- {
- process.ErrorDataReceived += Process_ErrorDataReceived;
- process.OutputDataReceived += Process_OutputDataReceived;
- process.StartInfo = psi;
- bool startResult = process.Start();
- StreamReader output = process.StandardOutput;
- StreamReader error = process.StandardError;
- Console.Write($"\rCommand process has started: {process.StartTime}\t\t\t\t\t");
- if (startResult)
- {
- //StreamReader output = process.StandardOutput;
- process.WaitForExit();
- //Console.WriteLine(output.ReadToEnd());
- Console.Write($"\rProcess has exited: Code {process.ExitCode} | {process.ExitTime}\t\t\t\t\t");
- }
- string outputDirectory = Path.Combine(OutputPath, "Logs");
- string errorPath = Path.Combine(outputDirectory, "ProcessErrorLog.txt");
- string outputPath = Path.Combine(outputDirectory, "ProcessOutputLog.txt");
- if (process.ExitCode != 0)
- {
- using (
- FileStream fs = new FileStream(errorPath,
- FileMode.Append,
- FileSystemRights.AppendData,
- FileShare.ReadWrite,
- 4096,
- FileOptions.Asynchronous))
- {
- string errorString = error.ReadToEnd();
- fs.WriteAsync(Encoding.UTF8.GetBytes(errorString), 0, errorString.Length);
- }
- }
- using (
- FileStream fs = new FileStream(outputPath,
- FileMode.Append,
- FileSystemRights.AppendData,
- FileShare.ReadWrite,
- 4096,
- FileOptions.Asynchronous))
- {
- string outputString = output.ReadToEnd();
- fs.WriteAsync(Encoding.UTF8.GetBytes(outputString), 0, outputString.Length);
- }
- bool hasTerminated = false;
- Stopwatch sw = Stopwatch.StartNew();
- while (!hasTerminated)
- {
- hasTerminated = process.HasExited;
- if (sw.Elapsed.Seconds >= 10)
- break;
- }
- sw.Stop();
- Console.Write("\rEnding Process Execution\t\t\t\t\t");
- return true;
- //return process.ExitCode == 0;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine($"\r\n{e.Message}\r\n{e}");
- throw;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement