Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a console application by typing EXIT?

Tags:

vb.net

console

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 close when they type EXIT and then press ENTER. What should I do?

like image 259
Rajdeep Avatar asked Jan 24 '26 13:01

Rajdeep


1 Answers

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.

like image 101
Michael Petrotta Avatar answered Jan 28 '26 16:01

Michael Petrotta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!