开发者

How to close a console application by typing EXIT?

I have an VB.Net Console Application running and I do not want it to close when the user hit the ENTER button, rather I want it to c开发者_运维知识库lose when they type EXIT and then press ENTER. What should I do?


Read from the console using Console.ReadLine() (which requires a carriage return to complete).

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜