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

        }

    }
  • 相关阅读:
    firefox打开链接自动跳转至新页面设置
    sql server 查询不为空的字段
    C# 判断ip地址是否正确
    Win7自带功能,刻录光盘遇到的问题
    【一起学源码-微服务】Nexflix Eureka 源码六:在眼花缭乱的代码中,EurekaClient是如何注册的?
    【一起学源码-微服务】Nexflix Eureka 源码五:EurekaClient启动要经历哪些艰难险阻?
    【一起学源码-微服务】Nexflix Eureka 源码四:EurekaServer启动之完成上下文构建及EurekaServer总结
    【一起学源码-微服务】Nexflix Eureka 源码三:EurekaServer启动之EurekaServer上下文EurekaClient创建
    【一起学源码-微服务】Nexflix Eureka 源码二:EurekaServer启动之配置文件加载以及面向接口的配置项读取
    【一起学源码-微服务】Netflix Eureka 源码一:Netflix Eureka 源码初探,我们为什么要读源码?
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/585259.html
Copyright © 2020-2023  润新知