Skip to content

Instantly share code, notes, and snippets.

@autinerd
Created March 25, 2019 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save autinerd/426da4fd15c386ab1b7423fbac92bff3 to your computer and use it in GitHub Desktop.
Save autinerd/426da4fd15c386ab1b7423fbac92bff3 to your computer and use it in GitHub Desktop.
Password entering without showing up on the console
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