public static Bus AddNewBus() { Console.WriteLine("Enter parameters for the new Bus:"); Console.WriteLine("model: "); string bname = Convert.ToString(Console.ReadLine()); Console.WriteLine("how many passenger seats: "); int bpassengers = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("fuel: "); string btank = Convert.ToString(Console.ReadLine()); Console.WriteLine("fuel expense per km: "); int bfuelExpense = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("price: "); int btotalPrice = Convert.ToInt32(Console.ReadLine()); var newBus = new Bus(bpassengers, bname, btank, bfuelExpense, btotalPrice); Bus.BinaryWriteToFile(newBus); return newBus; }
public static void BinaryWriteToFile(Bus bus) { const string filePath = "@//..//..//..//data//AddBus.dat"; BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { try { formatter.Serialize(fs, bus); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } finally { fs.Close(); } Console.WriteLine("Object serialized"); } }