支持掩码输入的 .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;
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值