/// <summary> /// Lecture d'un document /// </summary> /// <param name="file">information de fichier</param> /// <param name="result">objet obtenu</param> /// <returns>true if success</returns> public static bool Load(FileInfo file, out PersistentDataObject result) { result = null; if (file.Exists) { PersistentDataObject p; BinaryFormatter bf = new BinaryFormatter(); Object o; try { using (Stream s = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { o = bf.Deserialize(s); s.Close(); } } catch (SerializationException) { return(false); } if (o != null) { p = o as PersistentDataObject; if (p != null) { result = p; return(true); } else { return(false); } } else { return(false); } } else { return(false); } }
/// <summary> /// Sauvegarde d'un document /// </summary> /// <param name="file">information de fichier</param> /// <param name="data">objet à sérialiser</param> /// <param name="errorReason">error reason</param> /// <returns>true if success</returns> public static bool Save(FileInfo file, PersistentDataObject data, out string errorReason) { using (FileStream f = new FileStream(file.FullName, FileMode.Create, FileAccess.Write)) { BinaryFormatter bf = new BinaryFormatter(); try { bf.Serialize(f, data); f.Close(); errorReason = string.Empty; return(true); } catch (SerializationException e) { errorReason = e.Message; return(false); } } }