Guest User

Untitled

a guest
Nov 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. using System;
  2.  
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Runtime.Serialization;
  6.  
  7. namespace SerializationExample
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. BinaryFormatter bFormatter = new BinaryFormatter();
  14.  
  15. Dog dog1 = new Dog();
  16. dog1.name = "Fluor";
  17.  
  18. //Creating new FileStream which will create new file and give ability to write in it
  19. using (FileStream inFile = new FileStream("dog.dat", FileMode.Create, FileAccess.Write))
  20. {
  21. //Serialazing dog1 object and saving in inFile directory "dog.dat"
  22. bFormatter.Serialize(inFile, dog1);
  23. }
  24.  
  25. //Creating new FileStream which will open file and give ability to read it
  26. using (FileStream outFile = new FileStream("dog.dat", FileMode.Open, FileAccess.Read))
  27. {
  28. //Deserializng object hold in outfile directory "dog.dat" and replacing dog1 object
  29. dog1 = (Dog) bFormatter.Deserialize(outFile);
  30. }
  31.  
  32. //Checking if the file was deserialized from file correctly
  33. Console.WriteLine(dog1.name);
  34. Console.ReadKey();
  35. }
  36. }
  37. }
Add Comment
Please, Sign In to add comment