C # code implementation: enter the password on the console to display the asterisk

Source: Internet
Author: User

The content entered in the console C # is processed by string by default. It is difficult to display the asterisk if you enter the content once. However, if you enter only one character at a time, you can replace the character entered by the user with an asterisk!

First, the method in C # that allows users to press the next button is console. readkey (), the user can only press one button at a time, and its other overload method is console. readkey (bool B), a bool type parameter is used to control whether the user presses the keys on the console. Then, we can pass in the true parameter so that the button pressed by the user is not displayed on the console, so that the cursor stays at the original position, and then print a * sign at this position. The return value of this method is of the lelekeyinfo type. By querying msnd, it is found that the consolekeyinfo type is the console key that describes the press, including the characters represented by the console key and the status of changing the key shift, ALT, and CTRL. This data type is a struct type, which has two important attributes: Key and keychar. The key corresponds to a key used to be pressed, which is an enumeration type, and keychar is a Unicode Character corresponding to the key you press. In this way, I can get the user input string and press the user button. A user enters a password in a loop. When the user presses the key to enter the password, the user sets a non-display character, prints a * number at the same position, and finally determines whether the user has pressed the Enter key, in this way, the entire password input process is ended. The Code is as follows:

1 // enter the password on the console to display the asterisk 2 3 // define a string to receive user input 4 string input = NULL; 5 6 console. writeline ("Enter Password"); 7 8 while (true) 9 {10 // store the keys entered by the user, and do not display the character 11 consolekeyinfo ck = console at the input position. readkey (true); 12 13 // determines whether the user presses the Enter key 14 if (CK. key! = Consolekey. Enter) 15 {16 if (CK. Key! = Consolekey. backspace) 17 {18 // Save the characters entered by the user to the string 19 input + = ck. keychar. tostring (); 20 // Replace the character entered by the user with * 21 console. write ("*"); 22} 23 else24 {25 // Delete the wrong character 26 console. write ("\ B \ B"); 27} 28} 29 else30 {31 console. writeline (); 32 33 break; 34} 35} 36 37 console. writeline ("You just entered {0}", input); 38 39 console. readkey ();

The final implementation result is as follows:

 

C # code implementation: enter the password on the console to display the asterisk

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.