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

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

How to close a console application by typing EXIT?

vb.netconsole

提问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 EXITand 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 Mainmethod, 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 Mainmethod return.

请记住,一旦您从您的Main方法返回,您的控制台应用程序就会关闭。如果不查看应用程序的逻辑结构,很难准确地建议您做什么,但请记住:如果您想阻止应用程序退出,请不要让您的Main方法返回。