.net 运行exe后如何返回退出代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13927081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:47:43  来源:igfitidea点击:

How to return exit code after running an exe?

.netvbscript

提问by Code-EZ

I have created a Console application for validating a function and this application i need to execute by using vbscript. After executing this exe i want to return an exit code whether the function return success or not. How can i return a status or exit code in .net?

我创建了一个用于验证函数的控制台应用程序,我需要使用 vbscript 执行这个应用程序。执行此 exe 后,无论函数是否返回成功,我都想返回退出代码。如何在 .net 中返回状态或退出代码?

回答by gideon

I'm going to assume you're writing either C# or VB.NET. In either case usually people have a Main function that returns nothing, butyou can change this to return an integer to represent the exit code.

我将假设您正在编写 C# 或 VB.NET。在任何一种情况下,人们通常都有一个不返回任何内容的 Main 函数,您可以将其更改为返回一个整数来表示退出代码。

For C# see this MSDN page.

对于 C#,请参阅此 MSDN 页面

You can do:

你可以做:

static int Main()
{
    //...
    return 0;
}

For VB.NET see this MSDN page.

对于 VB.NET,请参阅此 MSDN 页面

You can do:

你可以做:

Module mainModule
    Function Main() As Integer
        '....
        '....
        Return returnValue
    End Function
End Module

回答by Paul Farry

In addition to @gideon you can also set

除了@gideon,您还可以设置

Environment.ExitCode = theExitCode;

In other parts of your code and exit directly if something really bad has happened

在代码的其他部分,如果发生了非常糟糕的事情,则直接退出

回答by Ekkehard.Horner

Given this C# program:

鉴于此 C# 程序:

class MainReturnValTest {
    static int Main(string[] args) {
        int rv = 0;
        if (1 == args.Length) {
            try {
                rv = int.Parse(args[0]);
            }
            catch(System.FormatException e)             {
                System.Console.WriteLine("bingo: '{1}' - {0}", e.Message, args[0]);
                rv = 1234;
            }
        }
        System.Console.WriteLine("check returns {0}.", rv);
        return rv;
    }
}

Sample runs:

示例运行:

check.exe
check returns 0.

check.exe 15
check returns 15.

check.exe nonum
bingo: 'nonum' Input string was not in a correct format.
check returns 1234.

and this VBScript script (reduced to the bare minimum, don't do this in production):

和这个 VBScript 脚本(减少到最低限度,不要在生产中这样做):

Option Explicit

Const WshFinished = 1


Dim goWSH : Set goWSH = CreateObject("WScript.Shell")

Dim sCmd : sCmd = "..\cs\check.exe"
If 1 = WScript.Arguments.Count Then sCmd = sCmd & " " & WScript.Arguments(0)
WScript.Echo sCmd
Dim nRet : nRet = goWSH.Run(sCmd, 0, True)
WScript.Echo WScript.ScriptName, "would return", nRet
With goWSH.Exec(sCmd)
    Do Until .Status = WshFinished : Loop
    WScript.Echo "stdout of check.exe ==>" & vbCrLf, .Stdout.ReadAll()
    nRet = .ExitCode
    WScript.Echo ".ExitCode of check.exe", nRet
End With
' !! http://stackoverflow.com/questions/2042558/how-do-i-get-the-errorlevel-variable-set-by-a-command-line-scanner-in-my-c-sha
WScript.Echo "Errorlevel:", Join(Array(goWSH.Environment("PROCESS")("ERRORLEVEL"), goWSH.ExpandEnvironmentStrings("%ERRORLEVEL%"), "???"), " - ")
WScript.Echo WScript.ScriptName, "returns", nRet
WScript.Quit nRet

Sample runs:

示例运行:

cscript 13921064.vbs
..\cs\check.exe
13921064.vbs would return 0
stdout of check.exe ==>
 check returns 0.

.ExitCode of check.exe 0
Errorlevel:  - %ERRORLEVEL% - ??? <=== surprise, surprise
13921064.vbs returns 0

echo %ERRORLEVEL%
0

cscript 13921064.vbs nonum & echo %ERRORLEVEL%
..\cs\check.exe nonum
13921064.vbs would return 1234
stdout of check.exe ==>
 bingo: 'nonum' Input string was not in a correct format.
check returns 1234.

.ExitCode of check.exe 1234
Errorlevel:  - %ERRORLEVEL% - ???
13921064.vbs returns 1234
0       <=== surprise, surprise

DNV35 E:\trials\SoTrials\answers927081\vbs
echo %ERRORLEVEL%
1234

You'll see

你会看到的

  • WScript.Quit is the way to return an exit code from your script
  • You start another process by using .Run or .Exec
  • .Run returns the exit code of the called process
  • .Exec sets .ExitCode (after termination!)
  • Accessing %ERRORLEVEL% in your script is futile (@LexLi)
  • cscript 13921064.vbs nonum & echo %ERRORLEVEL%is useless too
  • WScript.Quit 是从脚本中返回退出代码的方法
  • 使用 .Run 或 .Exec 启动另一个进程
  • .Run 返回被调用进程的退出代码
  • .Exec 设置 .ExitCode(终止后!)
  • 在脚本中访问 %ERRORLEVEL% 是徒劳的(@LexLi)
  • cscript 13921064.vbs nonum & echo %ERRORLEVEL%也没用

回答by Lex Li

As @gideon commented, in your executable you must use returnstatement to return the number.

正如@gideon 评论的那样,在您的可执行文件中,您必须使用return语句来返回数字。

In your script, please read %ERRORLEVEL%after calling this executable. That's the place Windows holds the return code.

在您的脚本中,请%ERRORLEVEL%在调用此可执行文件后阅读。这就是 Windows 保存返回码的地方。