因此,我需要在 Visual Studio 控制台中创建一个登录机制,使用我存储在文件中的用户名和密码对客户端进行身份验证,这是文件的内容:
1;user1;[email protected];username1;pass1 2;user2;[email protected];username2;pass2 3;user3;[email protected];username3;pass3 4;user4;[email protected];username4 ;pass4 5;user5;[email protected];username5;pass5
我以前从未使用过 c#。
先感谢您!
我已经尝试了以下代码,但我得到一个异常 null 说 arr[] 为 null:
string[] lines = File.ReadAllLines(@"c:/User.txt");
string[][] arr = new string[lines.Length][];
string username, password;
string fileuser = lines[3];
string filepassw = lines[4];
int userpos = 3;
int passwpos = 4;
bool isValideUser = false;
for (int x = 3; x >= 1; x--)
{
Console.WriteLine("You have " + x + " attempt/s.");
Console.Write("Enter Username>> ");
username = Console.ReadLine();
Console.Write("Enter Password>> ");
password = Console.ReadLine();
for (int row = 0; row < 3; row++)
{
if (username.Equals(arr[row][userpos]) && password.Equals(arr[row][passwpos]))
{
Console.WriteLine("Welcome " + lines[1] + "!");
isValideUser = true;
break;
}
}
if (!isValideUser)
{
Console.WriteLine("Invalid Input.");
if (x != 1)
{
Console.WriteLine("Please Try Again.");
Console.Write("\n");
}
else if (x.Equals(1))
{
Console.Write("Goodbye!");
break;
}
}
else
break;
}
Console.ReadKey();
您必须填充arr
值,我会更改arr
为类型List<string[]>
,然后List
使用foreach
and填充文件中的值string.Split()
:
安装于:
string[][] arr = new string[lines.Length][];
采用:
List<string[]> arr = new List<string[]>();
foreach (var item in lines)
{
arr.Add(item.Split(';'));
}
通过此修改,程序运行良好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句