public static bool Load(string filename, ref TestTime obj) { //десериализация из файла people.dat FileStream fs = null; try { fs = File.OpenRead(filename); BinaryFormatter formatter = new BinaryFormatter(); obj = (TestTime)formatter.Deserialize(fs); Console.WriteLine("Объект десериализован"); } catch (Exception ex) { System.Console.WriteLine(ex.Message); System.Console.WriteLine("Не удалось десериализовать файл, создан новый" + filename); obj.CreateList(); return(false); } finally { if (fs != null) { fs.Close(); } } return(true); }
public static bool Save(string filename, TestTime obj) { FileStream fs = null; try { fs = File.Open(filename, FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, obj); Console.WriteLine("Объект сериализован"); } catch (Exception ex) { System.Console.WriteLine(ex.Message); return(false); } finally { if (fs != null) { fs.Close(); } } return(true); }