vb.net 如何通过键入 EXIT 关闭控制台应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3819115/
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 to close a console application by typing EXIT?
提问by Rajdeep
I have an VB.Net Console Application running and I do not want it to close when the user hit the ENTERbutton, rather I want it to close when they type EXIT
and then press ENTER. What should I do?
我有一个 VB.Net 控制台应用程序正在运行,我不希望它在用户点击ENTER按钮时关闭,而是希望它在他们键入EXIT
然后按时关闭ENTER。我该怎么办?
回答by Michael Petrotta
Read from the console using Console.ReadLine()(which requires a carriage return to complete).
使用Console.ReadLine()从控制台读取(需要回车才能完成)。
If (Console.ReadLine() = "EXIT") Then
Environment.Exit(0) 'or, simply return from the Main method
End If
Another way to phrase it:
另一种表达方式:
While (Not Console.ReadLine() = "EXIT")
Console.WriteLine("Didn't say the magic word!")
End While
Remember that once you return from your Main
method, your console application closes. It's hard to advise you exactly what to do without seeing the logical structure of your application, but remember: if you want to prevent your application from exiting, don't let your Main
method return.
请记住,一旦您从您的Main
方法返回,您的控制台应用程序就会关闭。如果不查看应用程序的逻辑结构,很难准确地建议您做什么,但请记住:如果您想阻止应用程序退出,请不要让您的Main
方法返回。