congdantoancau

GET RESULT OUTPUT AND ERROR OUTPUT FROM COMMAND LINES

Nov 13th, 2020 (edited)
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. // C# GET RESULT OUTPUT AND ERROR OUTPUT FROM COMMAND LINES
  2. // From: https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output/29753402#29753402
  3.  
  4. static void runCommand()
  5. {
  6.     Process process = new Process();
  7.     process.StartInfo.FileName = "cmd.exe";
  8.     process.StartInfo.Arguments = "/c DIR"; // Note the /c command (*)
  9.     process.StartInfo.UseShellExecute = false;
  10.     process.StartInfo.RedirectStandardOutput = true;
  11.     process.StartInfo.RedirectStandardError = true;
  12.     process.Start();
  13.     //* Read the output (or the error)
  14.     string output = process.StandardOutput.ReadToEnd();
  15.     Console.WriteLine(output);
  16.     string err = process.StandardError.ReadToEnd();
  17.     Console.WriteLine(err);
  18.     process.WaitForExit();
  19. }
Advertisement
Add Comment
Please, Sign In to add comment