static void Main(string[] args) { Tutorial obj = new Tutorial(); obj.ID = 1; obj.Name = ".Net"; IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream("E:\\Example.txt", FileMode.Create, FileAccess.Write); formatter.Serialize(stream, obj); stream.Close(); stream = new FileStream("E:\\Example.txt", FileMode.Open, FileAccess.Read); Tutorial objNew = (Tutorial)formatter.Deserialize(stream); Console.WriteLine(objNew.ID); Console.WriteLine(objNew.Name); Console.ReadKey(); //Console.WriteLine("Hello World!"); }
static void Main(string[] args) { Tutorial sampleTutorial = new Tutorial(); sampleTutorial.id = 1; sampleTutorial.name = ".Net Tutorial"; IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream("C:\\your\\file\\path\\ishere\\result.txt", FileMode.Create, FileAccess.Write); formatter.Serialize(stream, sampleTutorial); stream.Close(); stream = new FileStream("C:\\your\\file\\path\\ishere\\result.txt", FileMode.Open, FileAccess.Read); Tutorial deserializeInfo = (Tutorial)formatter.Deserialize(stream); Console.WriteLine("Deserializing of the result.txt file "); Console.WriteLine("ID: " + deserializeInfo.id); Console.WriteLine("Name: " + deserializeInfo.name); Console.ReadKey(); }