如何使用文本文件检查密码是否与 c# 中的用户名匹配?
How to check if password matches username in c# using text files?
我是一名刚开始学习C#的学生,我想知道如何检查密码是否与文本文件中的用户名匹配。
static void CheckExist()
{
Console.WriteLine("Enter username: "); //ask the user for the username and store in a variable
string EuserName = Console.ReadLine();
Console.WriteLine("Enter password: "); //ask the user for the password and store in a variable
string EpassWord = Console.ReadLine();
string [] lines = File.ReadAllLines(filename); //read all lines of the file into an array
bool UserExist = false; //declares a Boolean variable to track if the user/password exists
string[] x;
foreach (string i in lines)//foreach string in the array
{
x = i.Split(","); //split the line on comma and store in an array X
if (x[0] == EuserName && x[2] == EpassWord) //if x index 0 equals the username and x index 2 equals the password
{
UserExist = true; // user exists, set the Boolean variable and exit loop
break;
}
} //end of loop
if (UserExist == false)
{
Console.WriteLine("User doesn't exist");
}
//if the Boolean variable is still false the user didn't exist, otherwise they did
}
这是更新后的代码,证明有效
执行以下逻辑:
//ask the user for the username and store in a variable
//ask the user for the password and store in a variable
//read all lines of the file into an array
//declare a Boolean variable to track if the user/password exists
//foreach string in the array
//split the line on comma and store in an array X
//if x index 0 equals the username and x index 2 equals the password
// user exists, set the Boolean variable and exit loop
//end of loop
//if the Boolean variable is still false the user didn't exist, otherwise they did
这是您在编码时应该做的:用您认为的语言写出逻辑,然后将其翻译成 C#。从您问题中的代码中,您已经知道如何执行上述所有操作
我是一名刚开始学习C#的学生,我想知道如何检查密码是否与文本文件中的用户名匹配。
static void CheckExist()
{
Console.WriteLine("Enter username: "); //ask the user for the username and store in a variable
string EuserName = Console.ReadLine();
Console.WriteLine("Enter password: "); //ask the user for the password and store in a variable
string EpassWord = Console.ReadLine();
string [] lines = File.ReadAllLines(filename); //read all lines of the file into an array
bool UserExist = false; //declares a Boolean variable to track if the user/password exists
string[] x;
foreach (string i in lines)//foreach string in the array
{
x = i.Split(","); //split the line on comma and store in an array X
if (x[0] == EuserName && x[2] == EpassWord) //if x index 0 equals the username and x index 2 equals the password
{
UserExist = true; // user exists, set the Boolean variable and exit loop
break;
}
} //end of loop
if (UserExist == false)
{
Console.WriteLine("User doesn't exist");
}
//if the Boolean variable is still false the user didn't exist, otherwise they did
}
这是更新后的代码,证明有效
执行以下逻辑:
//ask the user for the username and store in a variable
//ask the user for the password and store in a variable
//read all lines of the file into an array
//declare a Boolean variable to track if the user/password exists
//foreach string in the array
//split the line on comma and store in an array X
//if x index 0 equals the username and x index 2 equals the password
// user exists, set the Boolean variable and exit loop
//end of loop
//if the Boolean variable is still false the user didn't exist, otherwise they did
这是您在编码时应该做的:用您认为的语言写出逻辑,然后将其翻译成 C#。从您问题中的代码中,您已经知道如何执行上述所有操作