Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent to shell_exec

How execute command via shell and return the complete output as a string using C#?

equivalent to shell_exec() of PHP.

Thanks,advanced.

like image 749
The Mask Avatar asked Jun 21 '11 20:06

The Mask


2 Answers

The MSDN documentation on Process.StandardOutput documentation shows an example

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

You might like to extract the standard error stream as well.

like image 63
Michael Jacobsen Avatar answered Sep 27 '22 20:09

Michael Jacobsen


Use the Process class

like image 24
DaveShaw Avatar answered Sep 27 '22 22:09

DaveShaw