温馨提示×

c# readline怎样实现密码输入功能

c#
小樊
82
2024-06-24 10:54:31
栏目: 编程语言

在C#中,可以使用Console类的ReadKey方法来实现密码输入功能,示例如下:

using System;

class Program
{
    static void Main()
    {
        Console.Write("请输入密码:");
        string password = GetPassword();

        Console.WriteLine("\n您输入的密码是:" + password);
    }

    static string GetPassword()
    {
        string password = "";
        ConsoleKeyInfo key;

        do
        {
            key = Console.ReadKey(true);

            if (key.Key != ConsoleKey.Enter)
            {
                password += key.KeyChar;
                Console.Write("*");
            }
        }
        while (key.Key != ConsoleKey.Enter);

        return password;
    }
}

运行程序后,输入的密码将会以星号(*)显示,保护密码的安全性。

0