A.B.Cade
A.B.Cade

Reputation: 16905

ExitCode is always 0 when executing a .bat file

I'm usually not a C# person, so I hope it's not a stupid question...

I have a .bat file that looks like this (this is of course a simplified example):

file nn.bat

exit /B 3

When I run it from command I see that %ERRORLEVEL% is 3 (great!!)

I have this c# program:

C# Program

class Program
{
    static void Main(string[] args)
    {
        Process p = new Process();

        p.StartInfo.FileName = @"nn.bat";

        p.Start();
        p.WaitForExit();

        int rc = p.ExitCode;

        Console.WriteLine(rc);
    }
}

I expect rc to be 3, but it's always 0 no matter what I try ...

Where is my mistake ?

Upvotes: 3

Views: 2208

Answers (1)

A.B.Cade
A.B.Cade

Reputation: 16905

Finally, I've found a workaround:

In my batch file, instead of using exit /B 3 I used exit 3.

The idea (and explanation) came from this answer

I still can't explain how it worked for others with the /B option - my guess is that it has to do something with their operating system version.

Upvotes: 1

Related Questions