标签:
1 [Serializable] 2 public class Student 3 { 4 public Student(int age) 5 { 6 this.age = age; 7 } 8 9 private int age; 10 11 public string Name { get; set; } 12 13 }
1 static void Load() 2 { 3 using (FileStream fs = new FileStream("stu.bin", FileMode.Open)) 4 { 5 BinaryFormatter formatter = new BinaryFormatter(); 6 Student stu = formatter.Deserialize(fs) as Student; 7 } 8 9 Console.WriteLine("读取成功"); 10 } 11 12 static void Save() 13 { 14 Console.WriteLine("请输入姓名!"); 15 Student stu = new Student(10) { Name = Console.ReadLine() }; 16 using (FileStream fs = new FileStream("stu.bin", FileMode.Create)) 17 { 18 BinaryFormatter formatter = new BinaryFormatter(); 19 formatter.Serialize(fs, stu); 20 } 21 22 Console.WriteLine("保存成功"); 23 }
标签:
原文地址:http://www.cnblogs.com/zhouzhuang/p/5128919.html