zoukankan      html  css  js  c++  java
  • 从控制台读取password


    Tip :    从控制台读取password

    语言: C#

    ______________________________________________________________

    在登陆Linux系统的时候,体验过在Linux的shell命令行窗体中输入用户password吗? 以下体验下在Windows控制台中输入password的方式


    Showing  Effect



    SourceCode

            /// <summary>
            /// Read password from console
            /// </summary>
            /// <returns>password</returns>
            public static string ReadPassword()
            {
                char[] revisekeys = new char[3];
                revisekeys[0] = (char)0x08;
                revisekeys[1] = (char)0x20;
                revisekeys[2] = (char)0x08;
    
                StringBuilder sb = new StringBuilder();
                while (true)
                {
                    ConsoleKeyInfo kinfo = Console.ReadKey(true);
    
                    if (kinfo.Key == ConsoleKey.Enter)
                    {
                        break;
                    }
    
                    if (kinfo.Key == ConsoleKey.Backspace)
                    {
                        if (sb.Length != 0)
                        {
                            int rIndex = sb.Length-1;
                            sb.Remove(rIndex, 1);
                            Console.Write(revisekeys);
                        }
                        continue;
                    }
                    sb.Append(Convert.ToString(kinfo.KeyChar));
                    Console.Write("*");
                }
                return sb.ToString();
            }



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    django-高级
    django-模板
    django-视图
    django笔记一
    redis、mysql、mongodb数据库
    Scrapy-redis分布式+Scrapy-redis实战
    python2 'ascii'编码问题
    【java8新特性】方法引用
    java浮点数运算无法精确的问题
    java中Array和ArrayList区别
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4854870.html
Copyright © 2011-2022 走看看