Created
September 21, 2020 16:10
-
-
Save PrateekKumarSingh/3170c48eb20350310d871ff1ba7811fe to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Management.Automation; | |
namespace app { | |
class Program { | |
static void Main(string[] args) { | |
Console.Write ("Enter your password: "); | |
var password = string.Empty; | |
ConsoleKey key; | |
do { | |
var keyInfo = Console.ReadKey(intercept: true); | |
key = keyInfo.Key; | |
// if backspace is pressed and password string has chars | |
if (key == ConsoleKey.Backspace && password.Length > 0) { | |
Console.Write("\b \b"); | |
password = password.Substring(0,(password.Length-1)); | |
} | |
// if input char is not a control char like CR, LF | |
else if (!char.IsControl(keyInfo.KeyChar)) { | |
Console.Write("*"); | |
password += keyInfo.KeyChar; | |
} | |
// exit infinite loop when 'ENTER' is pressed | |
} while (key != ConsoleKey.Enter); | |
Console.WriteLine("\nYour Password is: "+password); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment