473,372 Members | 846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 11947
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gionni | last post by:
Hello everyone, I'm have a window application, but it is a console application too. I'll try to explain better: if I call the program without parameters the GUI starts. Otherwise it runs as a...
1
by: Reinier Beeckman | last post by:
I have made a simple console application which run through the Windows scheduled task. When the program is starten i get the console in the screen. Is it also possible to configure it that the...
2
by: CM | last post by:
I have C# .net console application compress image using leadtools library. I developed and tested on my machine - XP SP2, .NET Framework 1.1.4322, leadtools v13.0.1.69, and custom library that...
1
by: John Grandy | last post by:
When debugging a Console Application in Visual Studio 2003, how to specify the command line arguments ? static void Main(string args)
2
by: SharpCoderMP | last post by:
hi, i'm writing a console application and i would like to be able to safely retrive password from the user, so noone can see how it is being typed. consider this scenario: Console.Write("Enter...
1
by: kls_kumar | last post by:
Hi All, I have written a simple VB.Net Application which takes a .pst file and extracts the messages attachments from the .pst. It is working fine as a Console Application. I tried Web...
2
by: Patrick Hill | last post by:
Hi, I have a console application that takes command line arguments. Now it seems my program fails if the total number of characters of the input parameters is greater then 102 anyone know why and...
5
by: =?Utf-8?B?VkIgSm9ubmll?= | last post by:
How can I send the Enter or Return keystroke to a VB.Net 2005 console application? -- Coding in Sunny Central Florida
2
by: pankajprakash | last post by:
Hi All, I have a asp.net web service and I am running an .exe file though this web service. The web serivce is calling that .exe file (console application) and running the code of that console...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.