CMD 上未返回 ExitCode
ExitCode not returned on CMD
我目前正在开发一个 WPF 应用程序,它提供 ExitCode
s 可以在 Windows Eventlog
.
中找到
在某些情况下,我需要立即取消执行。
因此我使用 Environment.Exit(someInteger)
。
还需要通过批处理启动它并检查是否 ErrorLevel is NOT 0
.
通过 VisualStudio OutputConsole 输出:
The program '[6908] MyApp.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[6908] MyApp.vshost.exe' has exited with code 3 (0x3).
命令输出:
cd MyVisualStudioDebugDir
MyApp.exe
echo %errorlevel%
=> returns 0
为什么我调用的时候这里是0 Environment.Exit(3)
?
我不是 100% 确定,但这可能与您在脚本中调用 EXE 的方式有关。为了模仿您的情况,我创建了一个简单的 WPF 应用程序,它通过 Environment.Exit(3);
立即退出,然后是一个简单的 test.cmd
脚本:
@echo off
start /wait TestApp.exe
echo %ERRORLEVEL%
回声实际上是3
。
更新: 我注意到如果在没有 /wait
选项的情况下使用 start
,则 ERRORLEVEL 设置不正确。我相信这是因为 start
不会等待应用程序退出才继续。因此,您可以使用 start /wait TestApp.exe
或简单地使用 call TestApp.exe
。我已经更新了上面的例子。
On the command line, CMD.EXE does not wait for the application to
terminate and control immediately returns to the command prompt.
Within a command script CMD.EXE will pause the initial script and wait
for the application to terminate before continuing.
Description about cmd and exit codes
因此,将您的应用程序启动命令放入一个批处理文件,然后 运行 该批处理文件放入 cmd。
myBat.bat
MyApp.exe
echo %errorlevel%
错误级别将是正确的。
我目前正在开发一个 WPF 应用程序,它提供 ExitCode
s 可以在 Windows Eventlog
.
在某些情况下,我需要立即取消执行。
因此我使用 Environment.Exit(someInteger)
。
还需要通过批处理启动它并检查是否 ErrorLevel is NOT 0
.
通过 VisualStudio OutputConsole 输出:
The program '[6908] MyApp.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[6908] MyApp.vshost.exe' has exited with code 3 (0x3).
命令输出:
cd MyVisualStudioDebugDir
MyApp.exe
echo %errorlevel%
=> returns 0
为什么我调用的时候这里是0 Environment.Exit(3)
?
我不是 100% 确定,但这可能与您在脚本中调用 EXE 的方式有关。为了模仿您的情况,我创建了一个简单的 WPF 应用程序,它通过 Environment.Exit(3);
立即退出,然后是一个简单的 test.cmd
脚本:
@echo off
start /wait TestApp.exe
echo %ERRORLEVEL%
回声实际上是3
。
更新: 我注意到如果在没有 /wait
选项的情况下使用 start
,则 ERRORLEVEL 设置不正确。我相信这是因为 start
不会等待应用程序退出才继续。因此,您可以使用 start /wait TestApp.exe
或简单地使用 call TestApp.exe
。我已经更新了上面的例子。
On the command line, CMD.EXE does not wait for the application to terminate and control immediately returns to the command prompt.
Within a command script CMD.EXE will pause the initial script and wait for the application to terminate before continuing.
Description about cmd and exit codes
因此,将您的应用程序启动命令放入一个批处理文件,然后 运行 该批处理文件放入 cmd。
myBat.bat
MyApp.exe
echo %errorlevel%
错误级别将是正确的。