Masking console output

Posted on 2006-05-23
Last Modified: 2012-05-05
Im writing a console app, which requires the user to provide a password, what I want to do is to mask the output, preferably with just "", stars (*) would work fine too.

Example output #1 (What I want to avoid):
Enter password: 123456

Example output #2 (Prefered solution):
Enter password:

Example output #3 (Accepted solution):
Enter password: ******
Question by:bjqrn
    LVL 48

    Accepted Solution

    There is low-level support for this: read every character using Console::ReadKey(false) and optionally print * using Console.Write(Char) method. Stop when Enter is pressed.
    Simple way: set Console::ForegroundColor equal to Console::BackgroundColor and call ReadLine.
    LVL 49

    Expert Comment

    Sometime we forget that even in a Console app, it is possible to pop up a dialog box to get user input.   That's how I'd handle it.  

    I'd also check for something like
        -u:UserName -p:Password
    in the commandline parameters so the user could specify them for automatic (batch) operations.

    -- Dan
    LVL 49

    Expert Comment

    Two viable alternatives.  I recommend a split.
    LVL 3

    Author Comment

    The first one was more what I looked for.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now