By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,017 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,017 IT Pros & Developers. It's quick & easy.

System.Diagnostics.Process / .BAT file / always returns exit code 0

P: n/a
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

Here is some C# code:

Process p = new Process();
p.StartInfo.FileName =
@"C:\temp\ConsoleApplication1\ConsoleApplication1\ test1.bat";
// this doesn't work either:
//p.StartInfo.FileName = @"c:\windows\system32\cmd.exe";
//p.StartInfo.Arguments = @"/c
c:\temp\ConsoleApplication1\ConsoleApplication1\te st1.bat";
p.Start();
p.WaitForExit();
Console.WriteLine("Exit code = {0}", p.ExitCode);

I would expect this program to print "Exit code = 6"...
But it actually prints "Exit code = 0".

Why?

Thanks in advance for your input!
Eva Pierce Monsen

Jul 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
To somewhat answer my own question, I finally found this similar post:

http://groups.google.com/group/micro...ef61d0db8a24da

If I change test1.bat from "exit /B 6" to "exit 6", I get the expected
ExitCode.

But I'm unhappy with this; say someone wants to actually call test1.bat
from a command prompt (which in my real life app is always the case)?
"exit 6" will cause the command prompt window to close.

Can anyone think of a way to have test1.bat callable both from command
prompt and from the C# program and get a happy result either way?

Eva

Jul 12 '06 #2

P: n/a
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/
Jul 12 '06 #3

P: n/a
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/
Jul 13 '06 #4

P: n/a
ev********@gmail.com wrote:
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?
Generally, you set the errorlevel by executing a program. If that
program is the last command in the file, then it would be accessible to
a C# program invoking it.

-- Barry

--
http://barrkel.blogspot.com/
Jul 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.