Advertisement
thedrummonger

Console password

Sep 18th, 2016 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1.         public static string ConsolePassword()
  2.         {
  3.             string Password = "";
  4.             while (true)
  5.             {
  6.                 var key = Console.ReadKey(true);
  7.                 if (key.Key == ConsoleKey.Enter)
  8.                 {
  9.                     Console.WriteLine();
  10.                     break;
  11.                 }
  12.                 else if (key.Key == ConsoleKey.Backspace)
  13.                 {
  14.                     if (string.IsNullOrEmpty(Password)) { continue; }
  15.                     Console.Write("\b \b");
  16.                     Password = Password[0..^1];
  17.                 }
  18.                 else
  19.                 {
  20.                     Console.Write("*");
  21.                     Password += key.KeyChar;
  22.                 }
  23.             }
  24.             return Password;
  25.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement