The default configuration when launching a process is the child remains connected to the terminal of the parent process.
When the application shouldn't interact with the user, it is preferable to have an easy way to disconnect the child from the terminal.
This can done by adding the following properties to ProcessStartInfo:
// semantics of `</dev/null`
// This is: the child can read from standard in. Reading from standard in returns 0 bytes, i.e.: EOF.
// alternative names: NoStandardInput/CloseStandardInput/StandardInputEOF
bool EmptyStandardInput;
// semantics of `>/dev/null`
// This is: the child can write to standard out. Anything written to standard out is ignored.
bool DiscardStandardOutput;
// semantics of `2>/dev/null`
// This is: the child can write to standard error. Anything written to standard error is ignored.
bool DiscardStandardError;
// including request https://github.com/dotnet/corefx/issues/32387 in this API suggestion:
// semantics of `2>&1`
// This is: the parent will read both standard error and standard output from Process.StandardOutput.
// This means the ordering used by the child when writing to standard error/out is preserved. (This is lost when reading from Process.StandardOutput and Standard.Error separately).
bool RedirectStandardErrorToStandardOutput;
Example
This starts dotnet build as a child process.
We set EmtpyStandarInput to true. Should the process try to read from stdin, it will not cause a prompt on the terminal, and read an EOF.
We also set RedirectStandardErrorToStandardOutput to true. This makes it possible to read both stdout and stderr from Process.StandardOutput together in the order the application wrote to those file descriptors.
var psi = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = "build",
RedirectStandardErrorToStandardOutput = true,
EmptyStandardInput = true
};
using (var process = Process.Start(psi))
{
string output = process.StandardOutput.ReadToEnd();
}
CC @jhudsoncedaron
The default configuration when launching a process is the child remains connected to the terminal of the parent process.
When the application shouldn't interact with the user, it is preferable to have an easy way to disconnect the child from the terminal.
This can done by adding the following properties to
ProcessStartInfo:Example
This starts
dotnet buildas a child process.We set
EmtpyStandarInputtotrue. Should the process try to read from stdin, it will not cause a prompt on the terminal, and read an EOF.We also set
RedirectStandardErrorToStandardOutputtotrue. This makes it possible to read both stdout and stderr fromProcess.StandardOutputtogether in the order the application wrote to those file descriptors.CC @jhudsoncedaron