(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();
你在哪裏看到這個代碼中的問題?你是說「foreach」只發生一次,你期望它多次? – 2013-03-05 20:25:03
當我嘗試向我的文件添加一本書時,它會覆蓋當前在那裏的書。 – 2013-03-05 20:26:45
你能顯示聲明和'sw'的開頭嗎?這裏缺少一些東西。 – Steve 2013-03-05 20:33:47