473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
4 20233
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Erik Johnson | last post by:
Hi, I am trying to spawn a daemon type program to go off on its own and do some work (asynchoronously) from within an HTTPServer, but I am running into a problem where the web browser (actually...
1
by: Morten | last post by:
Hi! Does anyone know why the code below always returns an "access is denied" error? private void Page_Load(object sender, System.EventArgs e) { System.Diagnostics.Process meProc =...
1
by: Patrick | last post by:
When Tracing in ASP.NET, the IIS process (on IIs5.1) is locking on the Trace file, and I can't read the trace file without restarting the IIS: Even the following does NOT work (how could I fix...
0
by: Nurit N | last post by:
Hi, I have created a very simple batch file (echo hello world) and was trying to retrieve the standard output but every time I run the code it returns ExitCode as 1. The batch file runs just...
11
by: Nurit N | last post by:
This is the third newsgroup that I'm posting my problem. I'm sorry for the multiple posts but the matter becoming urgent. I hope this is the right place for it... I have created a very...
0
by: Daniel | last post by:
System.Diagnostics.Process.Start fails on windows server 2003 the process returns process.ExitCode == 0 but executing any process with System.Diagnostics.Process.Start on windows xp works fine....
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
2
by: test3 | last post by:
Hello folks, I'm using System.Diagnostics.Process to start a thirdparty program (that works perfectly when started via command line). I'm using Process.StandardOutput to get the output of the...
5
by: Saya | last post by:
Hi Folks, I have now spend app. 3 days to get the below scenario to work, but can not get there! ..Net version = 2.0.50727 Windows version = Microsoft Windows = Windows Server 2003 Now I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.