Read Password from C# Console Application

In this article, I am going to write C# Console Application (command prompt) to read password from the command-line screen. It also supports to remove character from entered password string while enter Backspace key.

 

static void Main(string[] args)
{
    Console.WriteLine("Password Masking Console Application");
    Console.WriteLine("------------------------------------");
    Console.Write("Enter username: ");
    string username = Console.ReadLine();
    string password = "";
    Console.Write("Enter password: ");
    ConsoleKeyInfo keyInfo;

    do
    {
        keyInfo = Console.ReadKey(true);
        // Skip if Backspace or Enter is Pressed
        if (keyInfo.Key != ConsoleKey.Backspace && keyInfo.Key != ConsoleKey.Enter)
        {
            password += keyInfo.KeyChar;
            Console.Write("*");
        }
        else
        {
            if (keyInfo.Key == ConsoleKey.Backspace && password.Length > 0)
            {
                // Remove last charcter if Backspace is Pressed
                password = password.Substring(0, (password.Length - 1));
                Console.Write("\b \b");
            }
        }
    }
    // Stops Getting Password Once Enter is Pressed
    while (keyInfo.Key != ConsoleKey.Enter);
    Console.WriteLine();
    Console.WriteLine("---------------------------");
    Console.WriteLine("Welcome " + username+",");
    Console.WriteLine("Your Password is : " + password);
}

Output: Password masking console application

Get Password from Console Application  -command-line

 


Advertisement

1 thought on “Read Password from C# Console Application”

Leave a Comment