如何检查用户输入是否与文本文件的内容匹配

时间:2019-03-26 16:05:48

标签: c# visual-studio

我正在构建一个用户可以登录的应用程序,然后该应用程序将检查用户的输入是否与文本文件中的输入匹配

我尝试使用StreamReader类,但我只是想不通。如果你们中有人觉得这是一个愚蠢的问题,我才刚刚开始学习c#,如果有人可以帮助我,我非常感谢。现在,问题是我想将用户输入的内容与文本文件的内容进行比较。在这种情况下,它是我自己的桌面。我怎么做?并且代码已经将键入的用户名和密码存储到文件中。我在上一个“ if”语句中尝试过。谢谢:)

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string username = Username.Text;
            string password = Password.Text;
            using (StreamWriter sw = new StreamWriter("C:\\Users\\omere\\Desktop\\Username.txt"))
            {
                sw.WriteLine(username);
                sw.WriteLine(password);
            }

            if (username != "C:\\Users\\omere\\Desktop\\Username.txt")
            {
                ErrorDisp.Text = "Login failed. Check if you have typed in the correct username and password";
            }

        }

1 个答案:

答案 0 :(得分:0)

尝试

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string username = Username.Text;
            string password = Password.Text;
            using (StreamWriter sw = new StreamWriter("C:\\Users\\omere\\Desktop\\Username.txt"))
            {
                sw.WriteLine(username);
                sw.WriteLine(password);
            }

           var lines = File.ReadAllLines("C:\\Users\\omere\\Desktop\\Username.txt");

            if (username != lines[0] || password != lines[1]  )
            {
                ErrorDisp.Text = "Login failed. Check if you have typed in the correct username and password";
            }

        }