如何在C#控制台应用程序中输入密码用掩码显示?

ysd_xwl 2009-03-18 11:44:55
在C#控制台应用程序中输入密码时用掩码显示,程序怎么写?
...全文
364 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
canlanrizi 2011-03-03
  • 打赏
  • 举报
回复
打多几个字了,额~
canlanrizi 2011-03-03
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string password = string.Empty;
ConsoleKeyInfo info;
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter && info.Key != ConsoleKey.Backspace && info.Key != ConsoleKey.Escape && info.Key != ConsoleKey.Tab && info.KeyChar != '\0')
{
password += info.KeyChar;
Console.Write('*');
}
/// <summary>
else
break; /// <summary>
} while (info.Key != ConsoleKey.Enter);
Console.WriteLine();
Console.WriteLine(password);

}


上面朋友的代码,我再加了一个判断,在测试上面朋友那个源代码的时候,“敲一下回车再输入,控制台并没有什么变化,之后再输入密码的时候,密码变成明文的了”,如果不输入正确密码就不让进入下面操作,可以把 break 改为 return ~
孙英雄 2009-03-19
  • 打赏
  • 举报
回复
补充下:
if
是判断按键是否为可打印字符键。
孙英雄 2009-03-19
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
string password = string.Empty;
ConsoleKeyInfo info;
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter && info.Key != ConsoleKey.Backspace && info.Key != ConsoleKey.Escape && info.Key != ConsoleKey.Tab && info.KeyChar != '\0')
{
password += info.KeyChar;
Console.Write('*');
}
} while (info.Key != ConsoleKey.Enter);
Console.WriteLine();
Console.WriteLine(password);

}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧