none
How to use StreamWriter and add text to the text file instead of overwrite the exsiting text? RRS feed

  • Question

  • I'm creating a login system with StreamWriter which is based on 'password only' login and the thing that I want to do is to keep add passwords that the users insert (by 'the users' I mean 'myself' because I'm doing it for fun and for personal use only...) to the same existing text file, so all the passwords will be arranged in the text file, one under the other.

    For example- User 1 inserts password: 123456789

                        User 2 inserts password: 1a2b3c

    Text file looks like this:                   123456789

                                                        1a2b3c

    The problem is that I'm using this code:

    using (StreamWriter sw = new StreamWriter(File.Create(filepath)))
                    {
                        sw.WriteLine(creationPassword);
                    }


    ('filepath' is a string that I attached to an empty .txt file and 'creationPassword' is the password that the user inserts as he signs up) What this code does is overwrite the file over and over again, every time I insert a new password. What do I need to change or write to make the program add passwords to the text file instead of overwriting the file again and again? And another question - How do I make the program (as the user tries to log in) go to that list of passwords and if the password that the user inserts to log in-exists, then he is 'signed in' to his account?    (Remember that the entire proccess is in the same code).

    I am trying to find a solution for these questions 5 hours already... Please help me... :`(

    Apologize in advance if I have any grammar mistakes...

    EDIT 1

    OK I searched a bit and I succeeded to add text without deleting anything using the following code:

    using (StreamWriter sw = File.AppendText(filepath))
                    {
                        sw.WriteLine(creationPassword);
                    }

    But I'm stiil stuck with the other part of my question- How do I make the program (as the user tries to log in) go to that list of passwords and if the password that the user inserts to log in-exists, then he is 'signed in' to his account?

    • Edited by yuvalmus Wednesday, April 4, 2018 10:16 PM
    Wednesday, April 4, 2018 9:13 PM

Answers

  • You can use StreamReader.

          static void Main(string[] args)
          {
             string filename = "c:\\als\\pass.txt";
    
             Console.WriteLine("Enter your password : ");
             string userPass = Console.ReadLine();
    
             bool passOkay = false;
             StreamReader reader = new StreamReader(filename);
             while (!reader.EndOfStream)
             {
                string pass = reader.ReadLine();
                if (pass == userPass)
                {
                   passOkay = true;
                }
             }
             reader.Close();
    
             if (passOkay)
                Console.WriteLine("Okay");
             else
                Console.WriteLine("Not okay");
          }

    A better way than reading the whole file every time would be to read it just once at the start and keep a list of valid passwords in an array or list. But I will leave that to you.

    • Proposed as answer by Fei HuModerator Thursday, April 5, 2018 6:25 AM
    • Marked as answer by yuvalmus Thursday, April 5, 2018 11:13 AM
    Thursday, April 5, 2018 12:02 AM
  • Hello yuvalmus,

    >>How do I make the program (as the user tries to log in) go to that list of passwords and if the password that the user inserts to log in-exists, then he is 'signed in' to his account?

    Or try the below code

      string input = "123";
      var passwords=File.ReadAllLines("../../TextFile1.txt");
      var IsVaild=passwords.Contains(input);
      if(IsVaild)
             ...
      else 
             ...
      Console.ReadLine();

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by yuvalmus Thursday, April 5, 2018 11:13 AM
    Thursday, April 5, 2018 6:25 AM
    Moderator

All replies

  • You can use StreamReader.

          static void Main(string[] args)
          {
             string filename = "c:\\als\\pass.txt";
    
             Console.WriteLine("Enter your password : ");
             string userPass = Console.ReadLine();
    
             bool passOkay = false;
             StreamReader reader = new StreamReader(filename);
             while (!reader.EndOfStream)
             {
                string pass = reader.ReadLine();
                if (pass == userPass)
                {
                   passOkay = true;
                }
             }
             reader.Close();
    
             if (passOkay)
                Console.WriteLine("Okay");
             else
                Console.WriteLine("Not okay");
          }

    A better way than reading the whole file every time would be to read it just once at the start and keep a list of valid passwords in an array or list. But I will leave that to you.

    • Proposed as answer by Fei HuModerator Thursday, April 5, 2018 6:25 AM
    • Marked as answer by yuvalmus Thursday, April 5, 2018 11:13 AM
    Thursday, April 5, 2018 12:02 AM
  • Hello yuvalmus,

    >>How do I make the program (as the user tries to log in) go to that list of passwords and if the password that the user inserts to log in-exists, then he is 'signed in' to his account?

    Or try the below code

      string input = "123";
      var passwords=File.ReadAllLines("../../TextFile1.txt");
      var IsVaild=passwords.Contains(input);
      if(IsVaild)
             ...
      else 
             ...
      Console.ReadLine();

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by yuvalmus Thursday, April 5, 2018 11:13 AM
    Thursday, April 5, 2018 6:25 AM
    Moderator
  • Thank you both! I finally finished this disheartening code...
    Thursday, April 5, 2018 11:16 AM