2015-03-23 15 views
10

ठीक है, तो मैं सी # के लिए नया हूं, लेकिन पहले से ही कुछ सीखा है। लेकिन मेरे पास एक सवाल है, मैं उन अक्षरों को कैसे बदलूं जिन्हें कंसोल में टाइप किया गया है "*" या बस उन्हें पूरी तरह छुपाएं?पासवर्ड टाइप करते समय छुपाएं/बदलें (सी #)

 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 :) 

किसी भी मदद की सराहना की जाएगी!

+1

http://www.c-sharpcorner.com/forums/thread/32102/password -इन-सी-शार्प-कंसोल-application.aspx –

उत्तर

17

पाया यह here

सी # कंसोल अनुप्रयोग में पासवर्ड मास्किंग

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; 
     } 
    } 
+0

तीर कुंजियों के साथ कुछ समस्याएं मिलीं लेकिन पर्याप्त अच्छी थीं। धन्यवाद! – Waescher

0
public static string HideCharacter() 
    { 
     ConsoleKeyInfo key; 
     string code = ""; 
     do 
     { 
      key = Console.ReadKey(true); 

      if (Char.IsNumber(key.KeyChar)) 
      { 
        Console.Write("*"); 
      } 
      code += key.KeyChar; 
     } while (key.Key != ConsoleKey.Enter); 

     return code; 

    } 

password = HideCharacter();

संबंधित मुद्दे