471,610 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,610 software developers and data experts.

How to show star when entering characters in console application?

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?

Nov 27 '06 #1
5 11370
On Mon, 27 Nov 2006 11:05:46 +0100, Hooyoo <zh*********@126.comwrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 27 '06 #2
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();

}
}
}

Nov 27 '06 #3

ga********@myway.com wrote:
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.

Nov 27 '06 #4
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
"Hooyoo" <zh*********@126.comwrote in message
news:11**********************@l39g2000cwd.googlegr oups.com...
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?
Nov 27 '06 #5

Morten Wennevik wrote:
On Mon, 27 Nov 2006 11:05:46 +0100, Hooyoo <zh*********@126.comwrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Easy way! Thanks.

Nov 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Reinier Beeckman | last post: by
1 post views Thread by John Grandy | last post: by
2 posts views Thread by SharpCoderMP | last post: by
5 posts views Thread by =?Utf-8?B?VkIgSm9ubmll?= | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.