[C#基礎]-C# Console下密碼輸入實做
[C#基礎]-C# Console下密碼輸入實做
因為昨天po了一篇java讀取密碼功能所以找了一下C# Console的版本,趕緊和同好分享。
static void Main(string[] args)
{
string LoginId = string.Empty;
Console.WriteLine(“************************\n”);
Console.WriteLine(“Enter the Login Name:”);
LoginId = Console.ReadLine();
Console.WriteLine(“Enter the Password:”);
StringBuilder pass = new StringBuilder();
char key;
while((key = Console.ReadKey(true).KeyChar)!=’\r’)
{
if (key == ‘\b’&& pass.Length > 0)
{
Console.Write(key + “” + key);
pass = pass.Remove(pass.Length – 1, 1);
}
else if (Char.IsLetterOrDigit(key))
{
Console.Write(“*”);
pass = pass.Append(key);
}
}
Console.WriteLine();
Console.WriteLine(“Thanks for Providing user name and password:”+pass.ToString());
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.ReadLine();
}
|