支持掩码输入的 .Net 2.0 Console 可用于使用掩码回显 password 的字符
.Net 1.1 下无法实现,终于在 .Net 2.0 可以解决了:
参阅了:
http://msdn2.microsoft.com/en-US/library/aa480477.aspx
增加了对: 方向键、Home键、End键 移动光标后插入字符的支持
增加了对: Delete键删除光标所在字符的支持
namespace Microshaoft
{
using System;
public class Class1
{
static void Main(string[] args)
{
Console.WriteLine("Enter password:");
string password = ConsoleReadMaskLine('*',true);
Console.WriteLine("\n" + password + "]");
password = ConsoleReadMaskLine('%',false);
Console.WriteLine("\n" + password + "]");
}

public static string ConsoleReadMaskLine
(
char PasswordChar
, bool WithMask
)
{
string password = "";
ConsoleKey ck;
string s = @"~!@#$%^&*()_+`1234567890-="; //可输入字符
s += @"QWERTYUIOP{}|qwertyuiop[]\";
s += "ASDFGHJKL:\"asdfghjkl;'";
s += "ZXCVBNM<>?zxcvbnm,./ ";

do
{
ConsoleKeyInfo cki = Console.ReadKey(true);
char c = cki.KeyChar;
ck = cki.Key;
int p = Console.CursorLeft;
if (ck == ConsoleKey.Backspace)
{
string left = "";
if (p > 0)
{
left = password.Substring(0,p - 1);
}
string right = password.Substring(p);
password = left + right;
Console.Write(c);

string output = right;
if (WithMask)
{
output = GetPasswordChars(right, PasswordChar);
}

output += "\0";
Console.Write(output);
if (p > 0)
{
p --;
}
}
else if (ck == ConsoleKey.Delete)
{
string left = "";
if (p > 0)
{
left = password.Substring(0, p);
}
string right = "";
if (p < password.Length)
{
right = password.Substring(p + 1);
}
password = left + right;
//Console.Write(right + " ");

string output = right;

if (WithMask)
{
output = GetPasswordChars(right, PasswordChar);
}
output += "\0";

Console.Write(output);
}
else
{
if (s.IndexOf(c) >= 0)
{
string left = password.Substring(0, p);
string right = password.Substring(p);
password = left + c + right;

string output = c + right;

if (WithMask)
{
output = GetPasswordChars(c + right, PasswordChar);
}
Console.Write(output);

p ++;
}
else
{
switch (ck)
{
case ConsoleKey.LeftArrow :
if (p > 0)
{
p --;
}
break;
case ConsoleKey.RightArrow :
if (p < password.Length)
{
p ++;
}
break;
case ConsoleKey.Home :
p = 0;
break;
case ConsoleKey.End :
p = password.Length;
break;
default :
Console.Beep();
break;
}
}
}
Console.CursorLeft = p;
} while (ck != ConsoleKey.Enter);
return password;
}
private static string GetPasswordChars(string s, char c)
{
string passwordChars = "";
for (int i = 0; i < s.Length; i++)
{
passwordChars += c;
}
return passwordChars;
}
}
}
参阅了:
http://msdn2.microsoft.com/en-US/library/aa480477.aspx
增加了对: 方向键、Home键、End键 移动光标后插入字符的支持
增加了对: Delete键删除光标所在字符的支持















































































































































【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决