C# 使用GetTickCount函数创建身份验证控制台应用程序
我正在尝试制作一个简单的登录/身份验证控制台应用程序,例如,我有一个字符串testpwd作为我的密码,我想让程序以毫秒为单位计算用户开始输入密码的时间,它应该输出每次用户在C# 使用GetTickCount函数创建身份验证控制台应用程序,c#,C#,我正在尝试制作一个简单的登录/身份验证控制台应用程序,例如,我有一个字符串testpwd作为我的密码,我想让程序以毫秒为单位计算用户开始输入密码的时间,它应该输出每次用户在GetTickCount函数的帮助下从键盘开始输入密码时,每个用户需要多少秒 我不知道我该怎么做,但我唯一能做到的就是下面的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System
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();
}
}
}
}
1-你可以使用DateTime。现在减去它们得到时间跨度 2-调用GetTickCount,但需要先声明它,如下所示:
[DllImport("kernel32.dll")]
static extern uint GetTickCount();
一个简单的?代码的相关部分可以这样编写
...
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();
}
.....
首先,你的问题很难理解。如果我理解正确,您希望在用户开始键入时开始计时,并在用户按enter键时停止计时?用电脑怎么样 在调用Console.ReadLine()之前,初始化一个新的秒表(),然后调用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";
用同样的答案击败我的道具:-)使用系统。诊断,无需参考新组件。它在System.Dll中