隐藏/输入密码时更换(C#) [英] Hide/replace when typing a Password (C#)

查看:188
本文介绍了隐藏/输入密码时更换(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我是新来的C#中,但已经了解了一些。但我有一个问题,我如何更换已经输入到一个控制台带*字符或只是隐藏起来完全?

Ok, So I am new to C#, but already learned a bit. But I have one question, how do I Replace Characters that are typed into a console with "*" or just hide them completely?

        var pw = "eric123";
        Console.WriteLine("Password: ");
        var value = Console.ReadLine();
        if (value == pw)
        {
            Console.WriteLine("Permitted, Play online? (Y/N)?");
            var getGameOnlineStatus = Console.ReadLine();

            //Rest Of the Code is just for me :)



任何帮助将不胜感激!

Any Help would be appreciated!

推荐答案

发现它的here

在C#控制台应用程序密码屏蔽

Password masking in C# console application

class PasswordExample
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Pls key in your Login ID");
            var loginid = Console.ReadLine();
            Console.WriteLine("Pls key in your Password");
            var password = ReadPassword();
            Console.Write("Your Password is:" + password);
            Console.ReadLine();
        }


     public static string ReadPassword()
        {
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    Console.Write("*");
                    password += info.KeyChar;
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        // remove one character from the list of password characters
                        password = password.Substring(0, password.Length - 1);
                        // get the location of the cursor
                        int pos = Console.CursorLeft;
                        // move the cursor to the left by one character
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                        // replace it with space
                        Console.Write(" ");
                        // move the cursor to the left by one character again
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    }
                }
                info = Console.ReadKey(true);
            }
            // add a new line because user pressed enter at the end of their password
            Console.WriteLine();
            return password;
        }
    }

这篇关于隐藏/输入密码时更换(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆