static bool Load(string filename, ref LaboratoryList obj) { FileStream fileStream = null; try { fileStream = File.OpenRead(filename); BinaryFormatter binaryFormatter = new BinaryFormatter(); obj = binaryFormatter.Deserialize(fileStream) as LaboratoryList; Console.WriteLine(" ===== booksLibrary\n" + obj); string error = binaryFormatter.Deserialize(fileStream) as string; if (fileStream != null) { fileStream.Close(); } return(true); } catch (Exception ex) { if (fileStream != null) { fileStream.Close(); } Console.WriteLine("Исключение: " + ex.Message); return(false); } }
static bool Save(string filename, LaboratoryList obj) { FileStream fileStream = null; try { fileStream = File.Create(filename); BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(fileStream, obj); if (fileStream != null) { fileStream.Close(); } return(true); } catch (Exception ex) { if (fileStream != null) { fileStream.Close(); } Console.WriteLine("Исключение: " + ex.Message); return(false); } }
static void Main(string[] args) { LaboratoryList laboratories = new LaboratoryList(); laboratories.AddDefaults(); Console.WriteLine(laboratories); Console.WriteLine(); Console.WriteLine("1: Максимальное число компьютеров в лаборатории -- {0}", laboratories.MaxComputers); Console.WriteLine(); Console.ReadKey(); Console.Write("2: Компьютер с максималльным числом процессоров:\n "); Console.WriteLine(laboratories.MaxProc.ToString()); Console.WriteLine(); Console.ReadKey(); Console.WriteLine("3: Принтеры в порядке возрастания года выпуска:"); foreach (Printer pr in laboratories.PrintersByYear) { Console.WriteLine(" " + pr.ToString()); } Console.WriteLine(); Console.ReadKey(); Console.WriteLine("4: Принтеры, сгруппированные по фирме-изготовителю:"); foreach (var group in laboratories.GroupByProducer) { Console.WriteLine("Producer of printers: " + group.Key); foreach (var pr in group) { Console.WriteLine(" " + pr.ToString()); } Console.WriteLine(); } Console.WriteLine(); Console.ReadKey(); Console.WriteLine("5: Одинаковые компьютеры, установленные в разных лабораториях:"); foreach (Computer comp in laboratories.AllRepeatingComputers) { Console.WriteLine(" " + comp.ToString()); } Console.WriteLine(); Console.ReadKey(); }
LaboratoryList DeepCopy() { LaboratoryList list_copy = null; MemoryStream memoryStream = null; using (memoryStream = new MemoryStream()) { try { BinaryFormatter binF = new BinaryFormatter(); binF.Serialize(memoryStream, this); memoryStream.Seek(0, SeekOrigin.Begin); list_copy = binF.Deserialize(memoryStream) as LaboratoryList; } catch (Exception ex) { Console.WriteLine(ex.Message); } } return(list_copy); }