title: How to loop through a file to get data?
body: <p>I am trying to make a basic login and sign up c# console application, however, I need to loop through my file to see if the username and password the user inputted has a match when logging in. If the user types in a username and password, I want my code to go through my file to check if it is an existing username and password</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Serializable]
  public class Users
  {
      public string UserName;
      public string Password;
  
      public Users(string userName, string password)
      {
          UserName = userName;
          Password = password;
      }
  }
  
  public class SaveToFile
  {
      public static void SerializeSignUpDetails(string userName, string password)
      {
          Users obj = new Users(userName, password);
          IFormatter formatter = new BinaryFormatter();
          Stream stream = new FileStream("SignUp.txt", FileMode.Append, FileAccess.Write);
          formatter.Serialize(stream, obj);
          stream.Close();
      }
      public static Users DeserializeSignUpDetails()
      {
          Stream stream = new FileStream("SignUp.txt", FileMode.Open, FileAccess.Read);
          IFormatter formatter = new BinaryFormatter();
          Users objnew = (Users)formatter.Deserialize(stream);
          stream.Close();
          return objnew;
      }
  }
  
  public static void Main(string[] args)
  {
      Console.WriteLine("To Login Type 1, To Create a new account Type 2");
      int LogInOrSignUp;
      do
      {
          int.TryParse(Console.ReadLine(), out LogInOrSignUp);
      } while (LogInOrSignUp != 1 &amp;&amp; LogInOrSignUp != 2);
  
      string userName = "";
      string password = "";
      bool successfull = false;
      Users userDetails = SaveToFile.DeserializeSignUpDetails();
      while (!successfull)
      {
          if (LogInOrSignUp == 1)
          {
              Console.WriteLine("Write your username:");
              userName = Console.ReadLine();
              Console.WriteLine("Enter your password:");
              password = Console.ReadLine();
              if (userName == userDetails.UserName &amp;&amp; password == userDetails.Password)
              {
                  Console.WriteLine("You have logged in successfully!");
                  successfull = true;
                  break;
              }
              if (!successfull)
              {
                  Console.WriteLine("Your username or password is incorect, try again!");
              }
          }
  
          else if (LogInOrSignUp == 2)
          {
              Console.WriteLine("Enter a username:");
              userName = Console.ReadLine();
  
              Console.WriteLine("Enter a password:");
              password = Console.ReadLine();
  
              successfull = true;
              SaveToFile.SerializeSignUpDetails(userName, password);
          }
      }
  }
  </code></pre>
  
  <p>I want to use foreach to loop through my file, but I am not sure how.</p>
  
  <p>Any help appreciated!</p>