0%

ASP.NET Core C# 模擬輸入密碼

C# Console 不顯示密碼

StackOverflow: C# Console - hide the input from console window while typing

這個解答更棒
StackOverflow: Password masking console application

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string pass = "";
do
{
ConsoleKeyInfo key = Console.ReadKey(true);
// Backspace Should Not Work
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
pass += key.KeyChar;
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
{
pass = pass.Substring(0, (pass.Length - 1));
Console.Write("\b \b");
}
else if(key.Key == ConsoleKey.Enter)
{
break;
}
}
} while (true);