There is a student class in which the name and password are checked (which are stored in a file). Adding students to the file goes through the Director class

class Student : User { public Student () { } public Student(string _name, string _surname, string _password) : base(_name, _surname, _password) { } public bool CheckPassword(string n, string s, string p) { name = n; surname = s; password = p; // TextReader tr = new StreamReader("D:\\Students\\Students.txt"); // String str = ""; // str = tr.ReadLine(); // foreach() } } 

Director class

 class Director : User { public Director(string _name, string _surname, string _password) : base(_name, _surname, _password) { } public void AddStudent() { Console.WriteLine("Имя:"); name = Console.ReadLine(); Console.WriteLine("Фамилия:"); surname = Console.ReadLine(); Console.WriteLine("Пароль:"); password = Console.ReadLine(); new Student(name, surname, password); using (var writer = new StreamWriter("D:\\Students\\Students.txt", true)) { //Добавляем к старому содержимому файла writer.WriteLine("Name: {0}\n Surname: {1}\n Password: {2}", name, surname, password); } } } 

How to check the information entered from the console with the file?

  • What specific verification do you want to do? - Indian
  • User enters name and password. If this data coincides with the database from the file (which are added by the director), then skip further, if not return 0 - Koki
  • To begin with, storing a password in clear text in a file is already wrong. Keep hashes from passwords, to add to the file write tool. - VladD
  • If my answer was helpful, please accept it. How does accepting an answer work - Indian

1 answer 1

After entering the data about the student, but before creating this student and adding an entry to the file, you must add a call to the input data verification function:

 Console.WriteLine("Имя:"); name = Console.ReadLine(); Console.WriteLine("Фамилия:"); surname = Console.ReadLine(); Console.WriteLine("Пароль:"); password = Console.ReadLine(); if (CheckExistingStudent(name, surname, "D:\\Students\\Students.txt") return 0; new Student(name, surname, password); 

Check function:

  private bool CheckExistingStudent(string _name, string _surname, string filePath) { bool result = false; string studentName = ""; string studentSurname = ""; StreamReader reader = null; try { reader = new StreamReader(filePath); while (reader.Peek() >= 0) { studentName = reader.ReadLine(); studentSurname = reader.ReadLine(); reader.ReadLine(); // считываем пароль, но не анализируем его studentName = studentName.Substring("Name: ".Length); studentSurname = studentSurname.Substring("Surname: ".Length); if (studentName == _name && studentSurname == _surname) { result = true; break; } } } catch (Exception e) { Console.WriteLine("Ошибка при проверке существования студента: {0}", e.Message); } finally { if (reader != null) reader.Close(); } return result; } 

It goes without saying that this function will work correctly only if the file with the list of students matches the rule described in your original message.