Solved

Hide password in Command Line - Console Application in Windows 2000

Posted on 2004-04-29
5
1,221 Views
Last Modified: 2007-12-19
Hi,

I am working on a commnad line Application in C# on Windows 2000. I want to let user enter the password on command prompt, How I can hide the typed characters (the password text), after asking to input?

Thanks
0
Comment
Question by:jss99
5 Comments
 
LVL 6

Expert Comment

by:prasitlee
ID: 10954157
Hi jss99,
    I think this link will bring you to the way to hide password on Commandline Applicaiton.
    http://www.dotnetspider.com/Technology/KBPages/22.aspx
    Any problem, please let me know.
                                                          Meng
0
 

Author Comment

by:jss99
ID: 10954650
Thanks, but this "ConsoleKeyInfo" thing does not work for me. I am using Framework 1.1 and VS .net 2003. Is there any special reference needs to be added?
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10956012
0
 
LVL 4

Expert Comment

by:pml_siva
ID: 10956386
using System;
using System.Text;

class a
{
      Console.Write("Input a password: ");
      StringBuilder sb = new StringBuilder();
      while (true)
      {

            ConsoleKeyInfo cki = Console.ReadKey(true);
            if (cki.Key == ConsoleKey.Enter)
            {
                  Console.WriteLine();
                  break;
            }
            
            if (cki.Key == ConsoleKey.BackSpace)
            {
                  if (sb.Length > 0)
                  {
                        Console.Write("\b\0\b");
                        sb.Length--;
                  }
                  
                  continue;
            }
            
            Console.Write('*');
            sb.Append(cki.KeyChar);
      }

      Console.WriteLine("Your password is: {0}", sb.ToString());
}

 
0
 
LVL 2

Accepted Solution

by:
Vaishali_chaudhry earned 125 total points
ID: 10956795
If i have understood the Q well...you are asking the user to enter the passwprd on the command prompt and then when he types the passwprd...you want the passwd not to be visible .

See the following sample:it uses ConsoleEx.dll

using System;
using Microsoft.GotDotNet;

namespace ConsoleApplication1
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  char c='a';
                  string s="";
                  Console.WriteLine("Enter your password:");
                  while(c!='q')
                  {
                        c=ConsoleEx.ReadChar();
                        Console.Write("*");
                        s+=c;
                  }
                  Console.WriteLine("Your password is :{0}",s);
                  Console.Read();
            }

            
      }
}

you can download ConsoleEx.dll from http://www.gotdotnet.com/community/usersamples/Default.aspx?query=ConsoleEx
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now