C#控制台-键入时从控制台窗口隐藏输入 [英] C# Console - hide the input from console window while typing

查看:110
本文介绍了C#控制台-键入时从控制台窗口隐藏输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Console.ReadLine 来读取用户的输入。但是,我想在键入时在控制台屏幕上隐藏/排除输入的文本。例如,当用户写 a时,它将 a写到控制台,然后我将得到一个值为 a的变量。但是,我不希望在控制台的输出上写上 a。

I'm using Console.ReadLineto read the input of the user. However, I want to hide/exclude the inputted text on the console screen while typing. For example, when the user writes "a", it writes "a" to the console and then I will have a variable with the value "a". However, I don't want "a" to be written on the console's output.

我该怎么做?

推荐答案

这是一个简短的实现。谢谢@ojblass

Here is a short implementation. thx @ojblass for the idea

System.Console.Write("password: ");
string password = null;
while (true)
{
    var key = System.Console.ReadKey(true);
    if (key.Key == ConsoleKey.Enter)
        break;
    password += key.KeyChar;
}

这篇关于C#控制台-键入时从控制台窗口隐藏输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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