C # Implementation in the console enter a password to display the Asterisk method _c# tutorial

Source: Internet
Author: User

The content entered in the console C # is handled by default, and it is difficult to achieve the function of displaying an asterisk if the user has entered it directly. However, if you allow users to enter only one character at a time, you can do this by replacing the character entered by the user with an asterisk!

First, the way in C # to let the user press a key is Console.readkey (), the user can only press one key at a time, in which one of its other overloaded methods is Console.readkey (bool b), Parameters of type bool are used to control whether a user presses a button on the console. Then we can pass in the true parameter, the user pressed the button does not appear on the console, so that the cursor always stay in the original position, and then print a * number in that position can be achieved. Also, the return value of this method is the Consolekeyinfo type. By checking msnd that the Consolekeyinfo type is the description of the pressed console key, including the characters represented by the console key and the state of the shift, ALT, and Ctrl modifier keys. This type of data type is a struct type, with two important properties key and Keychar. The key corresponds to a key that is pressed, an enumeration type, and Keychar is a Unicode encoded character corresponding to the user pressing the key. So I can get the string that the user entered and the button the user pressed. User input password is a cycle of the process, when the user presses the key to enter the password when the setting does not display characters, and then print a * number in the same position, and finally determine whether the user pressed the Enter, so that the entire password input process.

The specific code is as follows

The console input password displays

      an asterisk//defines a string to receive user input
      string input = null;

      Console.WriteLine ("Please enter the password");

      while (true)
      {
        //store the keystrokes entered by the user and do not display the characters in the position entered
        consolekeyinfo ck = Console.readkey (true);

        Determines whether the user presses the ENTER key if
        (CK). Key!= consolekey.enter)
        {
          if (CK). Key!= consolekey.backspace)
          {
            //the character entered by the user is stored in the string
            input = ck. Keychar.tostring ();
            Replace the character entered by the user with the *
            console.write ("*");
          }
          else
          {
            //delete the wrong character
            console.write ("\b \b");
          }
        else
        {
          Console.WriteLine ();

          break;
        }
      }

      Console.WriteLine ("You just entered is {0}", input);

      Console.readkey ();

The final effect is as follows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.