(Books 类是在此代码之外定义的。)因此,当我尝试从此文件中阅读一本书时,我只得到这本书。每次我添加一个新的,旧的都会被覆盖。所以我无法将新书添加到我的 myLibraryBooks 列表中,因为它会自动被覆盖。
Books book = new Books();
Console.Write("Enter Author Name:");
book.Author = Console.ReadLine();
Console.Write("Enter Book Title:");
book.Title = Console.ReadLine();
Console.Write("Enter Book ISBN:");
book.ISBN = Console.ReadLine();
Console.Write("Enter the Publish Date:");
book.Publish_Date = Console.ReadLine();
myLibraryBooks.Add(new Books() { Author = book.Author.ToUpper(), Title = book.Title.ToUpper(), ISBN = book.ISBN, Publish_Date = book.Publish_Date.ToUpper() });
Console.WriteLine("Book added Successfully");
sw.Write(book.Author, FILE_PATH);
sw.Write("\r\n", FILE_PATH);
sw.Write(book.Title.ToUpper(), FILE_PATH);
sw.Write("\r\n", FILE_PATH);
sw.Write(book.ISBN.ToUpper(), FILE_PATH);
sw.Write("\r\n", FILE_PATH);
sw.Write(book.Publish_Date.ToUpper(), FILE_PATH);
sw.Write("\r\n", FILE_PATH);
sw.WriteLine(book.Author, FILE_PATH);
sw.Close();