简体   繁体   English

如何使用文本文件检查密码是否与 c# 中的用户名匹配?

[英]How to check if password matches username in c# using text files?

I'm a student who just started learning C# and I want to know how to check to see if the password matches the username in a text file.我是一名刚开始学习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

        }

This is the updated code, proved working这是更新的代码,证明有效

Implement the following logic:实现以下逻辑:

//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

This is what you should do when you code: write the logic out in the language you think in and then translate it to c#.这就是你在编码时应该做的:用你思考的语言写出逻辑,然后将其翻译成 c#。 From the code in your question you already know how to do all the above从您问题中的代码中,您已经知道如何执行上述所有操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM