Friday, July 18, 2008

How to capture %ERRORLEVEL% value

In Windows OS, Application's return value is stored in a system variable %ERRORLEVEL%

Sample Code //filename.cs

using System;
namespace Test
{
class TEST
{
static int Main(string[] args)
{
Console.WriteLine("Hello");
foreach(string s in args)
{ Console.WriteLine(s);
}
Console.ReadLine();
return -1;
}
}

}
Compile the above file ex: csc /t:exe filename.cs

Create a batch file with the following content
@echo off

rem testing application

@if "%ERRORLEVEL%" == "0" goto success
:fail
echo This application has failed!
echo return value = %ERRORLEVEL%
goto end
:success
echo This application has succeeded!
echo return value = %ERRORLEVEL%
goto end
:end
echo All Done.


ECHO "END"

Now run the exe and the batch file.

No comments:

Post a Comment