Write Binary Using BinaryFormatter Class In C#

This snippet shows how to serialize an object using the BinaryFormatter Class and save the data to a file.


  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Stream bStream = File.Open("file.bin",FileMode.Create);
  9.         object[] employee1 = { "John" , "Doe" };
  10.         object[] employee2 = { "Tom" , "Baker" };
  11.         object[] contacts = { employee1, employee2  };
  12.         BinaryFormatter bFormater = new BinaryFormatter();
  13.         bFormater.Serialize(bStream, contacts);
  14.         Console.ReadKey();
  15.     }
  16. }

No comments:

Post a Comment