如何在不覆盖先前数据的情况下将数据添加到文件

时间:2020-06-03 23:48:06

标签: c# loops serialization

我正在尝试进行基本登录并注册c#控制台应用程序,但是,当我尝试创建新帐户时,它将覆盖之前创建的帐户。

这是我的代码:

[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);
                }
            }
        }

更新

感谢@DrkDeveloper帮助我解决覆盖问题!

但是现在我如何遍历文件中的所有用户名和密码,以检查用户登录时输入的用户名和密码是否匹配?

感谢到目前为止的所有帮助!

1 个答案:

答案 0 :(得分:2)

在序列化流中使用:

“ FileMode.Append”

如果仅打开或仅创建,则将其覆盖。

考虑以下事实:如果在文件中附加信息,则必须知道如何正确读取数据,找到每个附加文件的起始位置等等……

还有一个问题,为什么不使用“ using子句”?

编辑:

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();
}