C # is equivalent to shell_exec

How to execute a command through a shell and return the full output as a string using C #?

equivalent to shell_exec () PHP.

Thanks, advanced.

+5
source share
3 answers

The MSDN documentation in the 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 can also extract the standard error stream.

+4
source

Use the Process class

+6
source

All Articles