3

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 ?

leppie
  • 115,091
  • 17
  • 196
  • 297
A.B.Cade
  • 16,735
  • 1
  • 37
  • 53
  • 2
    It works correctly for me. Are you triple sure there's no mistake of some kind in the code? – Jon Nov 06 '12 at 09:47
  • You may also want to check this post: [Executing Batch File in C#](http://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp) – Artemix Nov 06 '12 at 09:50
  • @Jon, yes I'm sure... when you say *It works correctly for me* do you mean that `rc == 3` ? – A.B.Cade Nov 06 '12 at 09:52
  • @Jon, a mistery... can it be related to the OS? I'm using win XP sp3 – A.B.Cade Nov 06 '12 at 10:05

1 Answers1

1

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.

Community
  • 1
  • 1
A.B.Cade
  • 16,735
  • 1
  • 37
  • 53