Skip to content

Instantly share code, notes, and snippets.

@scolestock
Created September 30, 2016 17:02
Show Gist options
  • Save scolestock/8014a376df48d0a40e3ad16f2caa7529 to your computer and use it in GitHub Desktop.
Save scolestock/8014a376df48d0a40e3ad16f2caa7529 to your computer and use it in GitHub Desktop.
C# ReadLine with mask
// http://stackoverflow.com/questions/3404421/password-masking-console-application
private string ReadLineWithMask()
{
string pass = "";
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
pass += key.KeyChar;
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
{
pass = pass.Substring(0, (pass.Length - 1));
Console.Write("\b \b");
}
}
}
// Stops Receving Keys Once Enter is Pressed
while (key.Key != ConsoleKey.Enter);
return pass;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment