Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1.  private static Process _proc;
  2.         static Terminal() {
  3.             _proc = new Process();
  4.             _proc.EnableRaisingEvents = true;
  5.             _proc.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
  6.             _proc.ErrorDataReceived += (s,e) => Console.WriteLine(e.Data);
  7.         }
  8.  
  9.         public static void Run(string command) {
  10.             _proc.StartInfo = new ProcessStartInfo() {
  11.                 FileName = "/bin/bash",
  12.                 Arguments = $"-c \"{command}\"",
  13.                 UseShellExecute = false,
  14.                 RedirectStandardOutput = true,
  15.                 RedirectStandardError = true,
  16.                 RedirectStandardInput = true,
  17.             };
  18.             _proc.Start();
  19.  
  20.             while(!_proc.StandardOutput.EndOfStream){
  21.                 Console.WriteLine(_proc.StandardOutput.ReadLine());
  22.             }
  23.         }
  24.  
  25.             public static string Execute(string command) {
  26.             _proc.StartInfo = new ProcessStartInfo() {
  27.                 FileName = "/bin/bash",
  28.                 Arguments = $"-c \"{command}\"",
  29.                 UseShellExecute = false,
  30.                 RedirectStandardOutput = true,
  31.                 RedirectStandardError = true,
  32.                 RedirectStandardInput = true,
  33.             };
  34.             _proc.Start();
  35.             StringBuilder strgBuilder = new StringBuilder();
  36.             while(!_proc.StandardOutput.EndOfStream){
  37.                 strgBuilder.AppendLine(_proc.StandardOutput.ReadLine());
  38.             }
  39.             return strgBuilder.ToString();
  40.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement