Created
December 24, 2018 10:16
-
-
Save AnshulKuthiala/278fb2a5243cea17af7024b44e0c8e76 to your computer and use it in GitHub Desktop.
[Console App Password Entry]
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
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