Predator
Predator

Reputation: 1275

How to exit a console application when user type anything?

I have following VB.NET console application code:

Module Module1

    Sub Main()
        Dim UserInfo As String = "Name: User1"

        System.Console.WriteLine(UserInfo)
        System.Console.ReadLine()
    End Sub
End Module

How to exit the console application when user types anything?

UPDATE: This is the solution:

Module Module1

    Sub Main()
        Dim UserInfo As String = "Name: User1"

        System.Console.WriteLine(UserInfo)
        System.Console.ReadKey()
    End Sub

End Module

Thanks for all!

Upvotes: 2

Views: 806

Answers (3)

Pan Pizza
Pan Pizza

Reputation: 1034

Use this one:

System.Console.ReadKey()

It will read a keystroke and return to code. If there is no more code after the System.Console.ReadKey() line then it will exit the console application.

You can check the value of keystroke being pressed by user and decide whether to do what in next action. You can exit the console application immediate using Exit Sub.

Upvotes: 3

Steve B
Steve B

Reputation: 37710

Use System.Console.ReadKey() instead of System.Console.ReadLine()

Upvotes: 4

Oded
Oded

Reputation: 499352

Instead of:

System.Console.ReadLine()

Use:

System.Console.ReadKey()

The former (your code) waits for the user to enter a line.

Upvotes: 2

Related Questions