title: how to check if a user input already exists in a text file within c#
body: <p>i am making a c# application for my a a level computing coursework. my coursework requires me to have a registration and login  system and i am currently stuck with the registration part because my registration system is meant to check if a username chosen by the user already exists within the text file.</p>
  
  <p>i have already searched on stack overflow on a solution and have tried to implement similar solutions bu it dosent seem to work as my code still lets new users to register with an already existing password</p>
  
  <pre><code> string firstname = txtBoxFirstname.Text;
   string surname = txtBoxSurname.Text;
   string fullname = txtBoxFirstname.Text+" "+txtBoxSurname.Text;
   string username = txtBoxUsername.Text;
   string password = txtBoxPassword.Text;
   string confirmedPassword = txtBoxConfirmedPassword.Text;
   DateTime lastlogin = DateTime.Now;
   bool containsUser = File.ReadLines("users.txt").Contains(username);
  
  if ((!string.IsNullOrEmpty(firstname)) &amp;&amp; 
     ((!string.IsNullOrEmpty(surname)) &amp;&amp; 
     ((!string.IsNullOrEmpty(username)) &amp;&amp; 
     (!string.IsNullOrEmpty(password)) &amp;&amp; 
     (!string.IsNullOrEmpty(confirmedPassword)))))
  {
  
      if ((!username.Contains("~")) || (!password.Contains("~")))
      {
          if (password == confirmedPassword)
          {
              if (containsUser == false)//this is the part that check if a username already exists
              {
                  FileStream fileStream = new FileStream("users.txt", FileMode.Append, FileAccess.Write);
                  StreamWriter streamWriter = new StreamWriter(fileStream);
  
                  try
                  {
                      streamWriter.WriteLine(fullname + "~" + username + "~" + password + "~" + lastlogin);
  
                      MessageBox.Show("User registered successfully", "Registration Successful");
  
                      this.Hide();
                      var homeForm = new HomeForm();
                      homeForm.Closed += (s, args) =&gt; this.Close();
                      homeForm.Show();
  
                  }
                  catch (Exception)
                  {
                      MessageBox.Show("Error registering the user", "Please try again");
                  }
                  finally
                  {
                      streamWriter.Close();
                      fileStream.Close();
                  }
              }
              else
              {
                  MessageBox.Show("Sorry this username is already taken", "Please try again");
              }
          }
          else
          {
              MessageBox.Show("Passwords must match",
                          "Incorrect Details");
          }
      }
      else
      {
          MessageBox.Show("Username and password must not contain any special characters i.e. ~", "~ entered");
      }
  
  }
  else
  {
      MessageBox.Show("Please ensure all data is entered into the fields",
          "Details missing");
  }
  </code></pre>
  
  <p>my expected code should return a message saying that this username is already taken if the username entered already exists but my program still creates the user an account anyway</p>