0

The specific problem I am seeing when executing a cmd process with something like "del *.txt" where one of the 'txt' files is open and cannot be deleted, the cmd process will output a line of text (saying something like 'file in use, cannot delete file') to the console, but not to the StandardOutput or the StandardError. According to this question [ https://stackoverflow.com/a/320779/832705 ] from 2008, the answer is no, but I am wondering if that might have changed in the past 4 years, or if someone has since found a workaround way. Also, I might be misinterpreting that answer, it might mean CLR exceptions and not cmd exceptions.

here is my process setup/start code:

ProcessStartInfo psi = new ProcessStartInfo("cmd", string.Empty);
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
outputfilesw = new StreamWriter(outputfile, true);
try
{
    p.Start();
    p.BeginOutputReadLine();
    //work code
}
Community
  • 1
  • 1
stackuser83
  • 2,012
  • 1
  • 24
  • 41
  • 1
    I was under the impression that you had to call p.BeginErrorReadLine() to catch any output to StandardError stream. I could be wrong. It's been some time since I was tinkering in System.Diagnostics. – trope Nov 15 '13 at 21:27
  • @trope: yeah, that was it. really simple. you should put it in an answer – stackuser83 Nov 15 '13 at 22:45

2 Answers2

2

You just have to call p.BeginErrorReadLine() to start the asynchronous read of StandardError. Answer added at suggestion of OP.

trope
  • 181
  • 4
0

You can read the output, and you can process the text returned. So, you should be able to find the text that indicates an error, even if it doesn't land in the error output.

Also, it is important to note that only the process being run can determine which output stream gets a message. So, if the command you're using decides to send errors to the standard stream, no amount of OS or C# work will change that.

John Fisher
  • 22,355
  • 2
  • 39
  • 64