2013-03-16 59 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";