Thanks Barry - my mistake. I am becoming more educated in
distinguishing DOS "errorlevel" vs. process return code. Is it correct
to suspect that if I set "errorlevel" in a .BAT file, it would be
inaccessible from a C# program invoking it?
Good suggestion on writing a program to return the exit code; however,
the real-life .BAT file must eventually be able to run on machines that
do not have .NET installed. I wanted to unit-test the .BAT file with
NUnit, hence the C# code in my original post.
Of course I could do what you suggested in VB6 or C++, but... what a
mess; all I want to do is run a simple script. I think the take-home
lesson is don't use DOS scripts...
Thanks again,
Eva Pierce Monsen
Barry Kelly wrote:
ev********@gmail.com wrote:
I'm trying to run a .BAT file using System.Diagnostics.Process. I'm
having trouble getting Process.ExitCode to match up with what the .BAT
file returns.
Here are the contents of
C:\temp\ConsoleApplication1\ConsoleApplication1\te st1.bat:
exit /B 6
This, in fact, does not work. You can test it in cmd with the || or &&
operators:
test1.bat || echo "Non-zero result"
(prints nothing)
or
test1.bat && echo "Zero result"
(prints "Zero result")
This shows that even CMD notices that the batch file does in fact return
with a zero result.
You can get around it by writing a simple program which returns the
desired exit code. The exit code of a batch file is the exit code of the
last command executed in the batch file. So, write a program called
"return" or something, like this:
---8<---
class App
{
static int Main(string[] args)
{
if (args.Length == 0)
return 0;
return int.Parse(args[0]);
}
}
--->8---
... and call this with 'return 6' or whatever result you want.
-- Barry
--
http://barrkel.blogspot.com/