Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save OlegPetrenkoGit/63a619de98d257412d0557838fd9b4d6 to your computer and use it in GitHub Desktop.
Save OlegPetrenkoGit/63a619de98d257412d0557838fd9b4d6 to your computer and use it in GitHub Desktop.
Read/Write fromto Binary file
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