Carlos Liu
Carlos Liu

Reputation: 2438

How to get the return value of console app in a batch file?

I have a C# console application AAA.exe which can return an integer to indicate the result

static int Main(string[] args)
  {
    . . .
    if(case1)
       return -1;

    if(case2)
       Environment.Exit(1);

    return 0;
}

I will call AAA.exe in a batch file and need the return value

AAA.exe /p="param1"

My question is:

  1. how to get the return value of AAA.exe?
  2. is there any difference between return 0; and Environment.Exit(0); statements?

Upvotes: 3

Views: 6139

Answers (3)

YTG
YTG

Reputation: 1058

The environmental variable %ERRORLEVEL% contains the return code of the last executed program or script.

By default, the way to check for the ERRORLEVEL is via the following code.

Syntax
IF %ERRORLEVEL% NEQ 0 ( 
   DO_Something 
)

Source: https://www.tutorialspoint.com/batch_script/batch_script_return_code.htm

Upvotes: 0

Foole
Foole

Reputation: 4850

You can use "errorlevel" in your batch file to get the returned value. More info here.

Upvotes: 8

dugas
dugas

Reputation: 12433

Is there any difference between return 0; and Environment.Exit(0); statements?

See this post

Upvotes: 4

Related Questions