Windows Phone Developers

Wednesday, October 22, 2008

How to get the return value from C# program (console application)

Check Return Values of C# Program from a Batch File

Main() is void (returns nothing) by default; however, it can be made to return integer values as shown below

if (args.Length == 0)

{

Console.WriteLine("No Arguments Passed");

return -1;

}

else

{

Console.WriteLine("Following are the arguments");

foreach (string arg in args )

{

Console.WriteLine(arg);

}

return 0;

}

The above program will return error value (-1) if it doesn’t have any arguments.

The return value will be stored in an Environment Variable ERRORLEVEL. By checking the value of this variable, we can conclude the successful execution of the program

Here is the DOS batch file to check the Environment Variable – ERRORLEVEL

@echo off

rem CSharpSample "sample"

CSharpSample

@if "%ERRORLEVEL%" == "0" goto success

:fail

echo Execution Failed

echo return value = %ERRORLEVEL%

goto finally

: success

echo Execution Succeded

echo return value = %ERRORLEVEL%

goto finally

: finally

Pause

The above program returns error.

Passing a parameter to the program as shown below returns success.

@echo off

CSharpSample "sample"

@if "%ERRORLEVEL%" == "0" goto success

:fail

echo Execution Failed

echo return value = %ERRORLEVEL%

goto finally

: success

echo Execution Succeded

echo return value = %ERRORLEVEL%

goto finally

: finally

Pause




Return Value Succeeded

Error Value Returned from C# program
See also:

Output Parameters in C# (.NET)

How to Return Multiple Values from a VBA Function

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment