Skip to content

Instantly share code, notes, and snippets.

@AnshulKuthiala
Created December 24, 2018 10:16
Show Gist options
  • Save AnshulKuthiala/278fb2a5243cea17af7024b44e0c8e76 to your computer and use it in GitHub Desktop.
Save AnshulKuthiala/278fb2a5243cea17af7024b44e0c8e76 to your computer and use it in GitHub Desktop.
[Console App Password Entry]
public static string GetPassword()
{
string password = "";
do
{
ConsoleKeyInfo key = Console.ReadKey(true);
// Backspace Should Not Work
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
password += key.KeyChar;
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && password.Length > 0)
{
password = password.Substring(0, (password.Length - 1));
Console.Write("\b \b");
}
else if (key.Key == ConsoleKey.Enter)
{
break;
}
}
} while (true);
Console.WriteLine();
return password;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment