开发者

Why when a .bat file tests for %ERRORLEVEL% after running .NET app, it is testing for a string return type, not an int?

开发者 https://www.devze.com 2023-02-12 06:53 出处:网络
I was looking at how to run a .NET app from the 开发者_开发问答command line, or in a bat file, and this code was given for testing the %ERRORLEVEL% (the exiting return value) of the application:

I was looking at how to run a .NET app from the 开发者_开发问答command line, or in a bat file, and this code was given for testing the %ERRORLEVEL% (the exiting return value) of the application:

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

Why is it testing for "0" and not 0? As I understand it, the .NET executable is returning an int when it exits, not a string.


It's just coercing the numeric value to a string and comparing. This can be helpful if an environment variable has no value so %PARAM% == 1 would evaluate to == 1 which would cause an error.

For checking a program's ERRORLEVEL, it's better to evaluate as per Raymond Chen's blog.

IF ERRORLEVEL 1 ECHO error level is 1 or more
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号