Jump to content
  • Advertisement
Sign in to follow this  
JonathanCCC

making a password typed appear as ***** in C#

This topic is 3846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a C# console application and am wondering how I make a password entered appear as ****** so someone next to them couldn't see what it was, could somebody help me? also a side question I have just moved onto C# and notice that on the build menu there is no config manager like in C++/CLI to change from debug to release. Are you stuck with a bloated debug version of the executables in C#? thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
For your first question, take a look at the PasswordChar property of the TextBox control.

For your second question, there is indeed a configuration manager for debug and release mode settings. I'm not quite sure where you looked or why you couldn't find it.

Share this post


Link to post
Share on other sites
thanks for the help, but I still need to know how to do that in console applications using console.readline, if it's possible.

also i only have 3 options on the c# build menu, no config manager.

i have c# express 2005

Share this post


Link to post
Share on other sites
In the .NET 2.0 framework, you can use Console.ReadKey() to read a key input from the console and not display the key that was pressed. When doing so, you can then print a * yourself when you get input from the user.

Share this post


Link to post
Share on other sites
i had the same question about debug/release last weekend, in vc05 i believe the behavior is to compile both all the time, and run the debug executable (bin/debug) if you do Debug > Start Debugging (F5). There seems to automatically be a slightly smaller release executable under bin/release with no extra effort from me.

Share this post


Link to post
Share on other sites
Quote:
Original post by JonathanCCC
also a side question I have just moved onto C# and notice that on the build menu there is no config manager like in C++/CLI to change from debug to release. Are you stuck with a bloated debug version of the executables in C#?
Assuming VC# 2005 (and also assuming I remember this correctly [wink])

Under Tools > Options, make sure the little check box in the bottom left is checked (the "Show all settings" one) then go to Projects and Solutions. You should then be able to check a box to "Show advanced build configurations".

This should allow you to swap between debug and release [smile]

All the best,
ViLiO

Share this post


Link to post
Share on other sites
many thanks for the replies. I got the config manager working.

but I am having some problems with the Console.ReadKey and was wondering if you could point me in the right direction on how to use it. it's just for a simple guessing number game to learn some console stuff. and this part is for '2 player' so I don't want the person to see what was entered but i do want to display it later, right now my code with it is:

string firstpickstring = Console.ReadLine();
int firstpick = int.Parse(firstpickstring);
while (firstpick < 1 || firstpick > 10)
{
Console.WriteLine();
Console.WriteLine("Invalid choice\n\n");
Console.Write(p2 + " please pick a number 1 to 10: ");
firstpickstring = Console.ReadLine();
firstpick = int.Parse(firstpickstring);
}

which works fine but display the number, when I tried changing the readline to readkey it gives aload of errors that you can't use a string or int with it and stuff, how are you supposed to use it? Also I'm not sure how I would get the user input and make it put a * in place, could anyone help or point me to online resource? I am a real novice at this

Share this post


Link to post
Share on other sites
That's because ReadKey reads in a single key, not a string, and it returns a ConsoleKeyInfo. However, you can retrieve the char of it in the KeyChar parameter.

So basically, you want to get chars one at a time (printing a * each time) and then you want to put the chars together in a string, parse it into int and do whatever you do.

example


char C = 'a';
string szNumber = "";

do
{
C = Console.ReadKey(true).KeyChar;
szNumber += C;
Console.Write("*");
} while (C != 'q');

//make sure szNumber is correct number and not some random characters
int iNumber = int.Parse(szNumber.TrimEnd('q') );

Console.Write("You wrote: " + iNumber.ToString() );

Share this post


Link to post
Share on other sites
If you want a drop in replacement for Console.ReadLine() you'd need something like:

static string ReadLineAsPassword() {
ConsoleKey k;
string pass = "";
do {
ConsoleKeyInfo key_info = Console.ReadKey(true);
k = key_info.Key;
if (k != ConsoleKey.Enter) {
Console.Write('*');
pass += key_info.KeyChar;
}
} while (k != ConsoleKey.Enter);
Console.WriteLine();
return pass;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!