Read from standard error of child process

suggest change
var errors = new System.Text.StringBuilder();
var process = new Process
{
    StartInfo = new ProcessStartInfo
    {
        RedirectStandardError = true,
        FileName = "xcopy.exe",
        Arguments = "\"NonExistingFile\" \"DestinationFile\"",
        UseShellExecute = false
    },
            
};
process.ErrorDataReceived += (s, e) =>
{
    if (e.Data != null)
        errors.AppendLine(e.Data);
};
process.Start();
process.BeginErrorReadLine();
process.WaitForExit();

if (errors.Length > 0) // something went wrong
    System.Console.Error.WriteLine($"Child process error: \r\n {errors}");

ErrorDataReceived is raised with a null Data when the error stream closes. If the child also fills its standard-output buffer, redirect and read that stream too, or WaitForExit can block.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents