Created
July 12, 2016 11:16
-
-
Save OlegPetrenkoGit/63a619de98d257412d0557838fd9b4d6 to your computer and use it in GitHub Desktop.
Read/Write fromto Binary file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void WriteToBinaryFile < T > (string filePath, T objectToWrite, bool append = false) | |
{ | |
using(Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create)) | |
{ | |
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); | |
binaryFormatter.Serialize(stream, objectToWrite); | |
} | |
} | |
public static T ReadFromBinaryFile < T > (string filePath) | |
{ | |
using(Stream stream = File.Open(filePath, FileMode.Open)) | |
{ | |
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); | |
return (T) binaryFormatter.Deserialize(stream); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment