[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1248
  • Last Modified:

Hide password in Command Line - Console Application in Windows 2000

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
jss99
Asked:
jss99
1 Solution
 
prasitleeCommented:
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
 
jss99Author Commented:
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
 
pml_sivaCommented:
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
 
Vaishali_chaudhryCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now