1

I am trying to make a basic login and sign up c# console application, however, when i try making a new account it overwrites the previous account that was created.

Here is my 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.Create, 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 && 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 && 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);
                }
            }
        }

UPDATE

thanks @DrkDeveloper for helping me solve my overwriting problem!

But now how do I loop through all the usernames and passwords in my file to check if there is a match to the username and password the user inputs when logging in?

Thanks for all the help so far!

d51
  • 316
  • 1
  • 6
  • 23
  • 2
    `BinaryFormatter` [is not a good choice for persisting data](https://stackoverflow.com/a/30922179/22437). Use JSON, XML, a database, even flat text files are a better choice. – Dour High Arch Jun 03 '20 at 23:55
  • 1
    See also [What are the deficiencies of the built-in BinaryFormatter based .Net serialization?](https://stackoverflow.com/q/703073/3744182). – dbc Jun 04 '20 at 00:17

1 Answers1

2

In serialization stream use:

"FileMode.Append"

If you only Open or only Create you'll overwrite it.

Think about the fact if you append info in a file, you have to know how to read the data correctly, found where every append starts, etc... if you need it.

One more question, why are not using "using clauses"?

Edit:

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); //here is the point. you can use File.Append or File.Open static methods.
      formatter.Serialize(stream, obj);
      stream.Close();
}
DrkDeveloper
  • 939
  • 7
  • 17
  • How do I get FileMode.AppendOrCreate? – d51 Jun 03 '20 at 23:57
  • It's one of the enum existing enum values... My bad, is "Append" only https://learn.microsoft.com/es-es/dotnet/api/system.io.filemode?view=netcore-3.1 You have to check if files exists before – DrkDeveloper Jun 03 '20 at 23:58
  • ok, thanks it works! But At the moment when I try logging in, it will say wrong password or username unless I use the first username and password I created – d51 Jun 04 '20 at 00:02
  • "Think about the fact if you append info in a file, you have to know how to read the data correctly, found where every append starts, etc... if you need it." – DrkDeveloper Jun 04 '20 at 00:07