2013-03-16 46 views
0

我試圖做一個簡單的登錄/身份驗證控制檯應用程序,例如我有一個字符串testpwd作爲我的密碼,我想讓程序以毫秒計時用戶開始輸入密碼,並且應該輸出每個用戶每次用戶在鍵盤上使用GetTickCount函數的幫助開始鍵入時輸入密碼多少秒。使用GetTickCount函數創建身份驗證控制檯應用程序

我不知道我該怎麼去了解它,但我設法做的唯一的事情就是下面這段代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
namespace LoginSystem 
{ 
    class LSystem 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Hello! This is simple login system!"); 
      Console.Write("Write your username here: "); 
      string strUsername = Console.ReadLine(); 
      string strTUsername = "testuser"; 
      if (strUsername == strTUsername) 
      { 
       Console.Write("Write your password here: "); 
       Console.ForegroundColor = ConsoleColor.Black; 
       string strPassword = Console.ReadLine(); 
       string strTPassword = "testpwd"; 
       if (strPassword == strTPassword) 
       { 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("You are logged in!"); 
        Console.ReadLine(); 

       } 
       else 
       { 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("Bad password for user: {0}", strUsername); 
        Console.ReadLine(); 
       } 
      } 
      else 
      { 
       Console.WriteLine("Bad username!"); 
       Console.ReadLine(); 
      } 
     } 
    } 
} 

回答

0

簡單StopWatch?你的代碼的相關部分可以這樣寫:

... 
Console.ForegroundColor = ConsoleColor.Black; 
StopWatch sw = new Stopwatch(); 
sw.Start(); 
string strPassword = Console.ReadLine(); 
sw.Stop() 
TimeSpan ts = sw.Elapsed; 
string strTPassword = "testpwd"; 
if (strPassword == strTPassword) 
{ 
    Console.ForegroundColor = ConsoleColor.Gray; 
    Console.WriteLine("You are logged in after " + ts.Milliseconds.ToString() + " milliseconds"); 
    Console.ReadLine(); 
} 
..... 
+0

道具毆打我將它與同樣的答案:-) – theMayer 2013-03-16 15:47:07

+0

使用System.Diagnostics程序,無需引用新assemby。它在System.Dll中 – Steve 2013-03-16 16:26:30

0

1 - 您可以使用DateTime.Now然後減去他們得到的時間跨度。

2-調用GetTickCount的,但你必須先聲明它是這樣的:

[DllImport("kernel32.dll")] 
static extern uint GetTickCount(); 
0

首先,你的問題很難理解。如果我正確讀了你的話,你希望在用戶開始輸入時開始計時,並在用戶按下輸入時停止計時?如何使用System.Diagnostics.Stopwatch class

就在調用Console.ReadLine()之前,啓動一個新的Stopwatch(),然後調用Start()方法。

緊接着到Console.ReadLine(),停止秒錶:

 Console.Write("Write your username here: "); 

     var stopwatch = new System.Diagnostics.Stopwatch(); 
     stopwatch.Start(); 

     string strUsername = Console.ReadLine(); 

     stopwatch.Stop(); 

     string strTUsername = "testuser"; 
相關問題