Created
September 30, 2016 17:02
-
-
Save scolestock/8014a376df48d0a40e3ad16f2caa7529 to your computer and use it in GitHub Desktop.
C# ReadLine with mask
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
// 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