/// <summary> /// Save this document to the specified location /// in binary format. /// </summary> /// <param name="filePath">The filepath to save the document to</param> /// <returns>True if the file was successfully saved, else false</returns> public virtual bool SaveAs(FilePath filePath, DocumentSaveFileType type = DocumentSaveFileType.Binary) { #if !JS try { using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { if (type == DocumentSaveFileType.Binary) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, this); } else if (type == DocumentSaveFileType.Text) { var formatter = new UniqueFormatter(); formatter.Serialize(stream, this); } stream.Flush(); stream.Close(); } FilePath = filePath; //Store filepath LastSaved = DateTime.UtcNow; return(true); } catch (Exception ex) { throw new SerializationException("An error was encountered while attempting to save the document.", ex); //TODO: Notify user of error } #endif return(false); }
/// <summary> /// Load a document from a file stored in binary format /// </summary> /// <param name="filePath">The path of the file to be loaded.</param> /// <returns>The loaded document, if a document could indeed be loaded. /// Else, null.</returns> public static T Load <T>(FilePath filePath, DocumentSaveFileType type = DocumentSaveFileType.Binary) where T : Document { T result = null; #if !JS Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); stream.Seek(0, SeekOrigin.Begin); if (type == DocumentSaveFileType.Binary) { IFormatter formatter = new BinaryFormatter(); formatter.Binder = new CustomSerializationBinder(); result = formatter.Deserialize(stream) as T; } else if (type == DocumentSaveFileType.Text) { var formatter = new UniqueFormatter(); result = formatter.Deserialize(stream) as T; } result.FilePath = filePath; stream.Close(); #endif return(result); }
/// <summary> /// Load a document from a file stored in binary format /// </summary> /// <param name="filePath">The path of the file to be loaded.</param> /// <param name="type">The format of the document to load.</param> /// <returns>The loaded document, if a document could indeed be loaded. /// Else, null.</returns> public static T Load <T>(FilePath filePath, DocumentSaveFileType type = DocumentSaveFileType.Binary) where T : Document { return(Load <T>(filePath, new DocumentDeserializationOptions() { DocumentType = type })); }
/// <summary> /// Load a document from a remote strean in binary format /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream">Stream Object with file contents</param> /// <returns>The loaded document, if a document could indeed be loaded. /// Else, null.</returns> public static T Load <T>(Stream stream, DocumentSaveFileType type = DocumentSaveFileType.Binary) where T : Document { T obj = null; #if !JS obj = default(T); stream.Seek(0L, SeekOrigin.Begin); switch (type) { case DocumentSaveFileType.Binary: var formatter = (IFormatter) new BinaryFormatter(); formatter.Binder = new CustomSerializationBinder(); obj = formatter.Deserialize(stream) as T; break; case DocumentSaveFileType.Text: obj = new UniqueFormatter().Deserialize(stream, null) as T; break; } stream.Close(); #endif return(obj); }