語言: C#
在登陸Linux系統的時候,體驗過在Linux的shell命令行窗口中輸入用戶密碼嗎? 下面體驗下在Windows控制台中輸入密碼的方式
Showing Effect
/// <summary> /// Read password from console /// </summary> /// <returns>password</returns> public static string ReadPassword() { char[] revisekeys = new char[3]; revisekeys[0] = (char)0x08; revisekeys[1] = (char)0x20; revisekeys[2] = (char)0x08; StringBuilder sb = new StringBuilder(); while (true) { ConsoleKeyInfo kinfo = Console.ReadKey(true); if (kinfo.Key == ConsoleKey.Enter) { break; } if (kinfo.Key == ConsoleKey.Backspace) { if (sb.Length != 0) { int rIndex = sb.Length-1; sb.Remove(rIndex, 1); Console.Write(revisekeys); } continue; } sb.Append(Convert.ToString(kinfo.KeyChar)); Console.Write("*"); } return sb.ToString(); }
查看本欄目