I am trying to use ReadKey() to get a password field so it doesn't show in console for a .net core app I am running on Ubuntu.
This is code:
while (true)
{
var key = System.Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
break;
Io.RootPass += key.KeyChar;
}
But I get this exception:
Exception has occurred: CLR/System.InvalidOperationException
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Console.dll: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'
ReadLine() works fine. Why doesn't ReadKey() work?
Edit:
Actually Console.ReadLine() doesn't work either it just doesn't throw an exception. The return value is not assigned when enter hit so program sticks.
I am guessing this is because linux terminal works differently. Is there a different interface for linux terminal that works?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…