How to show star when entering characters in console application?

  • Thread starter Thread starter Hooyoo
  • Start date Start date
H

Hooyoo

Hi, here.
I write following codes:
string password = Console.ReadLine();

I want users enter their passwords, but readline will show content of
password when entering, so is there any way to show star(*) when
entering?
 
Hi, here.
I write following codes:
string password = Console.ReadLine();

I want users enter their passwords, but readline will show content of
password when entering, so is there any way to show star(*) when
entering?

Hi,

You can use

Console.ReadKey(true);
Console.Write("*");

but you will have to write some logic around it to handle system keys etc.
 
Hello, using google I have managed to find the following from another
online forum.
I hope it is useful, it may help you for now until someone that knows
the solution reads your post. I must stress I am a beginner, and am
merely reposting what someone else has posted here. But I hope it may
help you!
....

The console is created by default in line input mode. To do what you
want to
do you need to use GetConsoleMode and SetConsoleMode to turn off line
input
mode and character echo. The following is a quick and dirty
example(watch
for line wrap):

using System;

namespace Whatever
{
class Class1
{
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int SetConsoleMode(IntPtr hConsoleHandle,
int dwMode);

[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetConsoleMode(IntPtr hConsoleHandle,
ref int dwMode);

private const int ENABLE_LINE_INPUT = 2;
private const int ENABLE_ECHO_INPUT = 4;

private const int CONIN = 3;

[STAThread]
static void Main(string[] args)
{

IntPtr hStdIn = new IntPtr(CONIN);
int mode;
char inputChar;
string userName = null;
string password = "";

Console.WriteLine("Please enter your user name:");
userName = Console.ReadLine();

Console.WriteLine("Please enter your password:");

//Set console mode to read a character
//at a time and not echo input.
GetConsoleMode(hStdIn, ref mode);
mode = (mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
SetConsoleMode(hStdIn, mode);

//Read the password a character at a time.
do
{
inputChar = (char)Console.Read();
if (inputChar >= 32)
{
//Echo character with password mask.
password += inputChar;
Console.Write("*");
}
} while (inputChar != '\r');//Enter pressed end of password.

//Set console back to line input
//mode if that's what you want.
//mode = (mode | (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
//SetConsoleMode(hStdIn, mode);

Console.WriteLine("");

if (password == "Let me in")
{
Console.WriteLine("Welcome " + userName);
}
else
{
Console.WriteLine("Sorry, " + userName);
}
Console.Write("Press any key to continue...");
Console.Read();

}
}
}
 
I'm reasonably sure there isn't something already built in to .Net to do
this on the console, that being said the general idea would be to have a
loop, read in a character at a time and build your string. everytime the
user presses a key write out a *.

This does cause you to worry about things like arrow keys, home, end,
backspace, esc, etc, etc, etc ... Which is likely why in MS console apps
they don't try to display stars (e.g. net use, runas ...)

The good news is that in .Net 2.0 the Console class offers a lot to make
this kind of thing easier than previous versions.

Hope that helps,
-Chris
 
Hello, using google I have managed to find the following from another
online forum.
I hope it is useful, it may help you for now until someone that knows
the solution reads your post. I must stress I am a beginner, and am
merely reposting what someone else has posted here. But I hope it may
help you!
...

The console is created by default in line input mode. To do what you
want to
do you need to use GetConsoleMode and SetConsoleMode to turn off line
input
mode and character echo. The following is a quick and dirty
example(watch
for line wrap):

using System;

namespace Whatever
{
class Class1
{
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int SetConsoleMode(IntPtr hConsoleHandle,
int dwMode);

[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetConsoleMode(IntPtr hConsoleHandle,
ref int dwMode);

private const int ENABLE_LINE_INPUT = 2;
private const int ENABLE_ECHO_INPUT = 4;

private const int CONIN = 3;

[STAThread]
static void Main(string[] args)
{

IntPtr hStdIn = new IntPtr(CONIN);
int mode;
char inputChar;
string userName = null;
string password = "";

Console.WriteLine("Please enter your user name:");
userName = Console.ReadLine();

Console.WriteLine("Please enter your password:");

//Set console mode to read a character
//at a time and not echo input.
GetConsoleMode(hStdIn, ref mode);
mode = (mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
SetConsoleMode(hStdIn, mode);

//Read the password a character at a time.
do
{
inputChar = (char)Console.Read();
if (inputChar >= 32)
{
//Echo character with password mask.
password += inputChar;
Console.Write("*");
}
} while (inputChar != '\r');//Enter pressed end of password.

//Set console back to line input
//mode if that's what you want.
//mode = (mode | (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
//SetConsoleMode(hStdIn, mode);

Console.WriteLine("");

if (password == "Let me in")
{
Console.WriteLine("Welcome " + userName);
}
else
{
Console.WriteLine("Sorry, " + userName);
}
Console.Write("Press any key to continue...");
Console.Read();

}
}
}

Got it! Thank you very much.
 
Morten said:
Hi,

You can use

Console.ReadKey(true);
Console.Write("*");

but you will have to write some logic around it to handle system keys etc.
Easy way! Thanks.
 
Back
Top