`
niunan
  • 浏览: 698880 次
  • 性别: Icon_minigender_1
  • 来自: 南宁
社区版块
存档分类
最新评论

C#控制台应用程序中输入密码时用掩码显示

    博客分类:
  • .NET
阅读更多
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);

        }


if
是判断按键是否为可打印字符键。
分享到:
评论
1 楼 zk00790 2009-07-28  
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string password = string.Empty;
            ConsoleKeyInfo info;
            do
            {
                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("*");
                }
                if (info.Key == ConsoleKey.Backspace)
                {
                    password = password.Substring(0, password.Length - 1);
                    Console.Write(" ");
                }
            } while (info.Key != ConsoleKey.Enter);
            Console.WriteLine();
            Console.WriteLine(password);
        }
    }
}


稍微修改了一下,是他可以删除输入错误的字符~~~
不知道有没有什么错误

相关推荐

Global site tag (gtag.js) - Google Analytics