1

I have a console application written in c# which reads characters from Console.In, parses the characters and output the results to the Console.Out. It works fine except when the input is pipes in from another console command and that command exits with a code other than 0 in this case the exit code is always 0.

For example using the TestCommand.cmd script:

@echo off
echo Plain text

exit /b 1

Running this script give the following output:

>TestCommand.cmd
Plain text

>echo %ERRORLEVEL%
1

If I pipe the output to my console app I get a exit code 0

>TestCommand.cmd | MyConsoleApp.exe
Plain text

>echo %ERRORLEVEL%
0

Is it possible to determine the exit code from the "up stream" input in .Net and exit my app with the same code?

It would be acceptable even just to determine that it exited with a code != 0?

Preferably .Net 2?

bstoney
  • 6,594
  • 5
  • 44
  • 51
  • That `%ERRORLEVEL%` is giving the exit status of the current command/script not the previous one. – Rahul Oct 05 '15 at 07:14
  • That makes sense, what I would like to do is determine the exit code from the input stream and exit my app in a similar manner. – bstoney Oct 05 '15 at 07:27
  • 1
    Check this http://stackoverflow.com/questions/11170753/windows-command-interpreter-how-to-obtain-exit-code-of-first-piped-command – Rahul Oct 05 '15 at 07:29
  • 1
    This as well http://stackoverflow.com/questions/877639/how-do-i-get-the-error-level-of-commands-in-a-pipe-in-windows-batch-programming – Rahul Oct 05 '15 at 07:29

0 Answers0