C# 如何在 .NET 中指定控制台应用程序的退出代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/155610/
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
How do I specify the exit code of a console application in .NET?
提问by MrDatabase
I have a trivial console application in .NET. It's just a test part of a larger application. I'd like to specify the "exit code" of my console application. How do I do this?
我在 .NET 中有一个简单的控制台应用程序。它只是更大应用程序的测试部分。我想指定控制台应用程序的“退出代码”。我该怎么做呢?
采纳答案by TheSoftwareJedi
3 options:
3个选项:
- You can return it from
Main
if you declare yourMain
method to returnint
. - You can call
Environment.Exit(code)
. - You can set the exit code using properties:
Environment.ExitCode = -1;
. This will be used if nothing else sets the return code or uses one of the other options above).
Main
如果您将Main
方法声明为 return ,则可以返回它int
。- 你可以打电话
Environment.Exit(code)
。 - 您可以使用属性设置退出代码:
Environment.ExitCode = -1;
。如果没有其他设置返回码或使用上述其他选项之一,则将使用此选项)。
Depending on your application (console, service, web app, etc) different methods can be used.
根据您的应用程序(控制台、服务、Web 应用程序等),可以使用不同的方法。
回答by palehorse
int code = 2;
Environment.Exit( code );
回答by albertein
回答by Esteban Araya
Just return the appropiate code from main.
只需从 main 返回适当的代码。
int main(string[] args)
{
return 0; //or exit code of your choice
}
回答by crashmstr
Use ExitCode if your main has a void return signature, otherwise you need to "set" it by the value you return.
如果您的 main 有一个 void 返回签名,请使用 ExitCode,否则您需要通过您返回的值“设置”它。
If the Main method returns void, you can use this property to set the exit code that will be returned to the calling environment. If Main does not return void, this property is ignored. The initial value of this property is zero.
如果 Main 方法返回 void,则可以使用此属性设置将返回到调用环境的退出代码。如果 Main 不返回 void,则忽略此属性。此属性的初始值为零。
回答by Mark Brackett
In addition to the answers covering the return int's... a plea for sanity. Please, please define your exit codes in an enum, with Flags if appropriate. It makes debugging and maintenance so much easier (and, as a bonus, you can easily print out the exit codes on your help screen - you do have one of those, right?).
除了涵盖返回 int 的答案之外......请求理智。请在枚举中定义您的退出代码,如果合适,请使用标志。它使调试和维护变得更加容易(而且,作为奖励,您可以轻松地在帮助屏幕上打印出退出代码 - 您确实拥有其中之一,对吧?)。
enum ExitCode : int {
Success = 0,
InvalidLogin = 1,
InvalidFilename = 2,
UnknownError = 10
}
int Main(string[] args) {
return (int)ExitCode.Success;
}
回答by David
The enumeration option is excellent however can be improved upon by multiplying the numbers as in:
枚举选项非常好,但是可以通过将数字相乘来改进,如下所示:
enum ExitCodes : int
{
Success = 0,
SignToolNotInPath = 1,
AssemblyDirectoryBad = 2,
PFXFilePathBad = 4,
PasswordMissing = 8,
SignFailed = 16,
UnknownError = 32
}
In the case of multiple errors, adding the specific error numbers together will give you a unique number that will represent the combination of detected errors.
在多个错误的情况下,将特定的错误编号加在一起将为您提供一个唯一的编号,代表检测到的错误的组合。
For example, an errorlevel of 6 can only consist of errors 4 and 2, 12 can only consist of errors 4 and 8, 14 can only consist of 2, 4 and 8 etc.
例如,错误级别 6 只能包含错误 4 和错误 2,12 只能包含错误 4 和 8,14 只能包含错误 2、4 和 8 等。
回答by Aron Tsang
If you are going to use the method suggested by David, you should also take a look at the [Flags] Attribute.
如果您打算使用 David 建议的方法,您还应该查看 [Flags] 属性。
This allows you to do bit wise operations on enums.
这允许您对枚举进行位操作。
[Flags]
enum ExitCodes : int
{
Success = 0,
SignToolNotInPath = 1,
AssemblyDirectoryBad = 2,
PFXFilePathBad = 4,
PasswordMissing = 8,
SignFailed = 16,
UnknownError = 32
}
Then
然后
(ExitCodes.SignFailed | ExitCodes.UnknownError)
would be 16 + 32. :)
将是 16 + 32。:)
回答by Scott Munro
There are three methods that you can use to return an exit code from a console application.
您可以使用三种方法从控制台应用程序返回退出代码。
- Modify the
Main
method in your application so that it returns anint
instead ofvoid
(a function that returns anInteger
instead ofSub
in VB.Net) and then return the exit code from that method. - Set the Environment.ExitCodeproperty to the exit code. Note that method 1. takes precedence - if the
Main
method returns anything other thanvoid
(is aSub
in VB.Net) then the value of this property will be ignored. - Pass the exit code to the Environment.Exitmethod. This will terminate the process immediately as opposed to the other two methods.
- 修改
Main
应用程序中的方法,使其返回一个int
替代void
(在 VB.Net中返回一个Integer
替代的函数Sub
),然后从该方法返回退出代码。 - 将Environment.ExitCode属性设置为退出代码。请注意,方法 1. 优先 - 如果该
Main
方法返回void
(Sub
在 VB.Net 中为 a )以外的任何内容,则该属性的值将被忽略。 - 将退出代码传递给Environment.Exit方法。与其他两种方法相反,这将立即终止进程。
An important standard that should be observed is that 0
represents 'Success'.
应该遵守的一个重要标准是0
代表“成功”。
On a related topic, consider using an enumeration to define the exit codes that your application is going to return. The FlagsAttributewill allow you to return a combination of codes.
在相关主题上,考虑使用枚举来定义应用程序将返回的退出代码。该的FlagsAttribute将允许您返回码的组合。
Also, ensure that your application is compiled as a 'Console Application'.
此外,请确保您的应用程序被编译为“控制台应用程序”。
回答by Fred Mauroy
My 2 cents:
我的 2 美分:
You can find the system error codes here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
您可以在此处找到系统错误代码:https: //msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
You will find the typical codes like 2 for "file not found" or 5 for "access denied".
您会找到典型的代码,例如“未找到文件”的 2 或“拒绝访问”的 5。
And when you stumble on an unknown code, you can use this command to find out what it means:
当你偶然发现一个未知代码时,你可以使用这个命令来找出它的含义:
net helpmsg decimal_code
e.g.
例如
net helpmsg 1
网络帮助消息 1
returns
返回
Incorrect function
功能不正确