我在. net中有一个简单的控制台应用程序。这只是一个更大应用程序的测试部分。我想指定控制台应用程序的“退出代码”。我怎么做呢?


当前回答

系统错误码可在“系统错误码(0 ~ 499)”中查询。

你会发现典型的代码,如2表示“文件未找到”或5表示“访问被拒绝”。

当你偶然发现一个未知的代码时,你可以使用这个命令来找出它的意思:

net helpmsg decimal_code

例如,

net helpmsg 1

返回

Incorrect function

其他回答

有三种方法可用于从控制台应用程序返回退出代码。

Modify the Main method in your application so that it returns an int instead of void (a function that returns an Integer instead of Sub in VB.NET) and then return the exit code from that method. Set the Environment.ExitCode property to the exit code. Note that method 1. takes precedence - if the Main method returns anything other than void (is a Sub in VB.Net) then the value of this property will be ignored. Pass the exit code to the Environment.Exit method. This will terminate the process immediately as opposed to the other two methods.

应该遵守的一个重要标准是,0代表“成功”。

在相关主题中,考虑使用枚举定义应用程序将返回的退出代码。FlagsAttribute将允许您返回代码的组合。

此外,确保您的应用程序被编译为“控制台应用程序”。

三个选项:

如果你声明Main方法返回int,你可以从Main返回它。 您可以调用Environment.Exit(code)。 您可以使用属性设置退出代码:Environment。ExitCode = -1;。如果没有其他设置返回代码或使用上面的其他选项之一,将使用此选项)。

根据您的应用程序(控制台、服务、web应用程序等),可以使用不同的方法。

系统错误码可在“系统错误码(0 ~ 499)”中查询。

你会发现典型的代码,如2表示“文件未找到”或5表示“访问被拒绝”。

当你偶然发现一个未知的代码时,你可以使用这个命令来找出它的意思:

net helpmsg decimal_code

例如,

net helpmsg 1

返回

Incorrect function

使用这段代码

Environment.Exit(0);

如果你不想返回任何东西,就使用0作为int。

int code = 2;
Environment.Exit( code );