static void Main() { Books books = new Books(3); Book book = new Book("The Master Plan", 34.17m); books[0] = book; book = new Book("The Avenger", 45.23m); books[1] = book; book = new Book("Granite Falls", 14.78m); books[2] = book; books.PrintBooks(); PrintAverageBookPrice(34.17m, 45.23m, 14.78m); // Create file to use in serializing. string bookFullFileName = @"..\debug\GraniteFalls.dat"; // Open a file and serialize the Granite Falls book. using (FileStream bookFile = new FileStream(bookFullFileName, FileMode.Create, FileAccess.Write, FileShare.None)) { BinaryFormatter binFormat = new BinaryFormatter(); binFormat.Serialize(bookFile, book); } Console.WriteLine("\nThe following book has been serialized:\n{0}", book.ToString()); book = null; // Open a file and deserialize the Granite Falls book. using (FileStream bookFile = File.OpenRead(bookFullFileName)) { // TODO: Deserialize the book stored in bookFile. } Console.WriteLine("\nThe following book has been deserialized:\n{0}", book.ToString()); Console.Write("\n\nPress <ENTER> to end: "); Console.ReadLine(); }
static void Main() { Books books = new Books(3); Book book = new Book("The Master Plan", 34.17m); books[0] = book; book = new Book("The Avenger", 45.23m); books[1] = book; book = new Book("Granite Falls", 14.78m); books[2] = book; books.PrintBooks(); PrintAverageBookPrice(34.17m, 45.23m, 14.78m); // Create file to use in serializing. string bookFullFileName = @"..\debug\GraniteFalls.dat"; // Open a file and serialize the Granite Falls book. using (FileStream bookFile = new FileStream(bookFullFileName, FileMode.Create, FileAccess.Write, FileShare.None)) { BinaryFormatter binFormat = new BinaryFormatter(); binFormat.Serialize(bookFile, book); } Console.WriteLine("\nThe following book has been serialized:\n{0}", book.ToString()); book = null; // TODO: Open a FileStream type file for reading in a using block. Name of file is contained // in the variable bookFullFileName. Deserialize the contents of the file and set // the reference in the variable named book (declared above). using (FileStream bookFile = File.OpenRead(bookFullFileName)) { BinaryFormatter binFormat = new BinaryFormatter(); book = (Book)binFormat.Deserialize(bookFile); } Console.WriteLine("\nThe following book has been deserialized:\n{0}", book.ToString()); Console.Write("\n\nPress <ENTER> to end: "); Console.ReadLine(); }
static void Main() { Books books = new Books(3); Book book = new Book("The Master Plan", 34.17m); books[0] = book; book = new Book("The Avenger", 45.23m); books[1] = book; book = new Book("Granite Falls", 14.78m); books[2] = book; books.PrintBooks(); PrintAverageBookPrice(34.17m, 45.23m, 14.78m); // Create file to use in serializing. string bookFullFileName = @"..\debug\GraniteFalls.dat"; // Open a file and serialize the Granite Falls book. using (FileStream bookFile = new FileStream(bookFullFileName, FileMode.Create, FileAccess.Write, FileShare.None)) { // TODO: Serialize the Granite Falls book. } Console.Write("\n\nPress <ENTER> to end: "); Console.ReadLine(); }