C#控制台明/暗文输入
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace PasswordTest 7 { 8 class Program 9 { 10 /// <summary> 11 /// 输入 12 /// </summary> 13 /// <param name="isPassword">是否密码</param> 14 /// <param name="passwordChar">密码替换字符</param> 15 /// <returns>返回输入结果</returns> 16 static string Input(bool isPassword, char passwordChar = '*') 17 { 18 string result = string.Empty; 19 20 Console.Write("请输入: "); 21 22 //密码 23 if (isPassword) 24 { 25 char c; 26 while ((c = Console.ReadKey(true).KeyChar) != '\r') 27 { 28 result += c; 29 Console.Write(passwordChar); 30 } 31 Console.WriteLine(); 32 } 33 //普通输入 34 else 35 result = Console.ReadLine(); 36 37 return result; 38 } 39 40 //入口 41 static void Main(string[] args) 42 { 43 Console.Write("请选择输入类型(明文/暗文:Y/N)"); 44 45 Console.WriteLine("\n您输入的结果为:{0}", 46 Console.ReadLine().ToUpper().Trim() == "Y" ? 47 Input(false) : Input(true)); 48 49 Console.Write("按任意键退出"); 50 Console.ReadKey(true); 51 } 52 53 }//end class 54 }//end namespace
//欢迎转载,请注明原创,感谢
Never give up!