Sign in to follow this  

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

This topic is 3627 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
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
Quote:
Original post by SiCrane
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;
}


Ew.


static string ReadLineAsPassword() {
string password = ""; // as a Pythonista, calling it 'pass' messes with my head ;)
while (true) {
ConsoleKeyInfo key_info = Console.ReadKey(true);
if (key_info.Key == ConsoleKey.Enter) {
Console.WriteLine();
return password ;
}
Console.Write('*');
password += key_info.KeyChar;
}
}


(EDIT: Actually, this isn't good enough; what about backspace?)

Share this post


Link to post
Share on other sites
thanks alot for all of your help everyone.

I have used the last bit of code posted here and it seems to work ok. The only problem I have now is I do not know how to pass the result of password back to a variable in Main() so I can use it, before the value was in the "firstpick" variable, but now it's in the password variable in ReadLineAsPassword(). I need it to go into a variable called firstpick just after the "ReadLineAsPassword()" call in Main. Just incase you don't know what I mean, heres my source code:


using System;
using System.Collections.Generic;
using System.Text;


namespace GuessNumber
{
class Program
{
static string ReadLineAsPassword()
{
string password = "";
while (true)
{
ConsoleKeyInfo key_info = Console.ReadKey(true);
if (key_info.Key == ConsoleKey.Enter)
{
Console.WriteLine();
return password;
}
Console.Write('*');
password += key_info.KeyChar;
}
}
static void Main(string[] args)
{
Console.WriteLine(" -------------------------------");
Console.WriteLine(" | WELCOME TO GUESS NUMBER |");
Console.WriteLine(" -------------------------------");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Please select game type:\n\nType S for singleplayer or M for multiplayer");
string sm = Console.ReadLine();
sm = sm.ToUpper();
while (sm != "S" && sm != "M")
{
Console.WriteLine();
Console.WriteLine("Invalid choice\n\nType S for singleplayer or M for multiplayer");
sm = Console.ReadLine();
sm = sm.ToUpper();
}
if (sm == "M")
{
MP();
}
if (sm == "S")
{
SP();
}
}
static void SP()
{
Console.WriteLine("TO DO SP");
}
static void MP()
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Multiplayer chosen");
Console.WriteLine();
Console.Write("Player 1 please enter your name: ");
string p1 = Console.ReadLine();
Console.WriteLine();
Console.Write("Player 2 please enter your name: ");
string p2 = Console.ReadLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Game will consist of 4 rounds");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(p1 + " will guess first");
Console.WriteLine();
Console.Write(p2 + " please pick a number 1 to 10: ");

ReadLineAsPassword();

while (firstpick < 1 || firstpick > 10)
{
Console.WriteLine();
Console.WriteLine("Invalid choice\n\n");
Console.Write(p2 + " please pick a number 1 to 10: ");
ReadLineAsPassword();
}
Console.WriteLine();
Console.WriteLine(p2 + " has picked.");
Console.WriteLine();
Console.Write(p1 + " please enter a guess 1 to 10: ");
string firstguessstring = Console.ReadLine();
int firstguess = int.Parse(firstguessstring);
while (firstguess < 1 || firstguess > 10)
{
Console.WriteLine();
Console.WriteLine("Invalid choice\n\n");
Console.Write(p1 + " please enter a guess 1 to 10: ");
firstguessstring = Console.ReadLine();
firstguess = int.Parse(firstguessstring);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Results for round 1:");
Console.WriteLine();
Console.WriteLine(p2 + " picked " + firstpick);
Console.WriteLine();
Console.WriteLine(p1 + " guessed " + firstguess);
Console.WriteLine();
if (firstpick == firstguess)
{
Console.WriteLine(p1 + " wins round 1!");
}
else
{
Console.WriteLine(p2 + " wins round 1!");
}
}
}
}

Share this post


Link to post
Share on other sites

This topic is 3627 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this