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

Started by
10 comments, last by JonathanCCC 16 years, 3 months ago
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
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.
Mike Popoloski | Journal | SlimDX
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
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.
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.
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
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
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
int firstPick = Convert.ToInt32(Console.ReadKey(true).KeyChar);Console.Write('*');while (firstPick < 1 || firstPick > 10){    ...}
Mike Popoloski | Journal | SlimDX
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() );
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
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;    }

This topic is closed to new replies.

Advertisement