Created
March 25, 2019 09:23
-
-
Save autinerd/426da4fd15c386ab1b7423fbac92bff3 to your computer and use it in GitHub Desktop.
Password entering without showing up on the console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string password = ""; | |
string passwordPrompt = "Password: "; | |
Console.Write(passwordPrompt); | |
while (true) | |
{ | |
ConsoleKeyInfo key = Console.ReadKey(true); | |
if (key.Key == ConsoleKey.Enter) | |
{ | |
break; | |
} | |
else if (key.Key == ConsoleKey.Backspace) | |
{ | |
password = password.Length == 0 ? "" : password.Remove(password.Length - 1, 1); | |
} | |
else | |
{ | |
password += key.KeyChar; | |
} | |
} | |
Console.WriteLine(); | |
Console.WriteLine(password); | |
string unused = Console.ReadLine(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment