Skip to content

Instantly share code, notes, and snippets.

@fresky
Created August 24, 2012 10:01
Show Gist options
  • Save fresky/3448591 to your computer and use it in GitHub Desktop.
Save fresky/3448591 to your computer and use it in GitHub Desktop.
Serialize using BinaryFormatter
Example example = new Example();
example.ID = 10;
example.NameList.Add(new NamePair(){Name = "name1", Value = "value1"});
example.NameList.Add(new NamePair() { Name = "name2", Value = "value2" });
Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
Console.WriteLine("Writing Employee Information");
bformatter.Serialize(stream, example);
stream.Close();
example = null;
//Open the file written above and read values from it.
stream = File.Open("EmployeeInfo.osl", FileMode.Open);
bformatter = new BinaryFormatter();
Console.WriteLine("Reading Employee Information");
example = (Example)bformatter.Deserialize(stream);
stream.Close();
Console.WriteLine("Employee Id: {0}", example.ID.ToString());
Console.WriteLine("Employee Name: {0}", example.NameList.Count);
foreach (var s in example.NameList)
{
Console.WriteLine(s.Name);
Console.WriteLine(s.Value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment