• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Masking console output

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: ******
  • 2
1 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.
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
Two viable alternatives.  I recommend a split.
bjqrnAuthor Commented:
The first one was more what I looked for.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now