C#控制台 用*号显示密码输入

ContractedBlock.gif ExpandedBlockStart.gif Code
 static void Main(string[] args)
        {
            ArrayList al 
= new ArrayList();
            ConsoleKeyInfo cki 
= Console.ReadKey(true);
            
while (cki.Key.ToString() != "Enter")
            {
                al.Add(cki);
                Console.Write(
"*");
                cki 
= Console.ReadKey(true);
            }

            Console.WriteLine();

            
foreach (object o in al)
            {
                cki 
= ((ConsoleKeyInfo)o);
                Console.Write(cki.KeyChar);
            }
            Console.ReadLine();
        }
    }

//泛型实现:

        
static void Main(string[] args)
        {
            List
<ConsoleKeyInfo> ls = new List<ConsoleKeyInfo>();
            ConsoleKeyInfo cki 
= Console.ReadKey(true);
            
while (cki.Key.ToString() != "Enter")
            {
                ls.Add(cki);
                Console.Write(
"*");
                cki 
= Console.ReadKey(true);
            }

            Console.WriteLine();

            
foreach (ConsoleKeyInfo o in ls)
            {
                Console.Write(o.KeyChar);
            }
            Console.ReadLine();

转载于:https://www.cnblogs.com/zhangzufa/archive/2009/04/29/1446574.html

你可能感兴趣的:(c#)