1. Saturday, February 09, 2008 12:17:11 AM by Bill
    This is handy. I needed a masked input field, so it saved me some work.

    The ReadKey method returns both character keystrokes (letters, numbers, etc.) and function keystrokes (function keys, cursor movement keys, etc.). Since function keys don't actually provide characters that could be part of a password, it would be best to filter them out, but unfortunately, the ConsoleKeyInfo structure does not provide a property to classify whether a keystroke is a character or function key. The documentation does say that the KeyChar property provides the character *if one is available*. It turns out that if one is not available, KeyChar is set to the "zero" value of a character - the same thing you'd get if you did "(char)0" or "new char()". I put in a test for that and it seems to be filtering appropriately.
  2. Wednesday, September 24, 2008 2:48:40 PM by David
    really handy. I'm gonna use it (personal use) for a small utility to unlock BitLocked volumes in W2k8

  3. Friday, January 30, 2009 11:36:13 PM by Lars Brandt
    Great! Thanks for the post.
  4. Friday, July 31, 2009 9:23:41 AM by tjrobinson
    Great code and explanation, thanks
  5. Friday, October 02, 2009 3:08:53 PM by Lukasz
    it doesn't work with Hi-ASCII nor DBCS because console is not utf8
  6. Monday, July 05, 2010 8:04:19 PM by Northerner
    I was looking to mask a password in the Console in the easiest possible way, and I came up with this:

    string username = Console.ReadLine();
    //ensure the password is not visible
    ConsoleColor cul = Console.ForegroundColor;
    Console.ForegroundColor = Console.BackgroundColor;
    string password = Console.ReadLine();
    Console.ForegroundColor = cul;

    Changing the ForegroundColor to match the background ONLY affects the current console line. You dont see any characters because the text is the same color as the background.
  7. Thursday, July 08, 2010 7:55:50 PM by Raithe
    This is more Northerner. The issue with only changing the foreground/background color is that you can simply highlight the text and you can see it.
  8. Friday, April 29, 2011 6:22:30 PM by Brian
    You saved me from writing this from scratch. Very useful. Thank you.
  9. Tuesday, February 07, 2012 6:46:26 PM by rnaga
    You can find another implementation at
Add Comment