使用1键重启控制台程序或与所有其他程序一起退出

时间:2015-10-16 18:08:56

标签: vb.net

我是Visual Basic的新手,如果用户输入“1”则想重新启动控制台程序,如果他们按任何其他键则退出

我已将“Console.ReadLine”与If语句结合使用,但问题是我不希望他们在输入后按Enter键。

如果这是一个基本问题(没有双关语),任何帮助都会非常感激和抱歉

1 个答案:

答案 0 :(得分:1)

您可以使用ReadKey代替使用ReadLine。这不会等待输入。

'Clear any keys from the input buffer
While Console.ReadKey(True)
End While

Dim MyKey = Console.ReadKey(True)
If MyKey.KeyChar = "1"c Then
  'We can act on the key since they pressed 1 (let the main application repeat)
Else
   'They pressed something else, we want to exit the application
End If