标签:
把对象保存在文件中(格式为二进制文件):
IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(@"D:\MyObj.bin", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); MyObj myObj = new MyObj(); //To Do: 在这里对myObj的属性进行设置 formatter.Serialize(stream, myObj); stream.Close();
从文件还原对象:
IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(@"D:/MyObj.bin", FileMode.Open,FileAccess.Read, FileShare.None); MyObj myObj = (MyObj)formatter.Deserialize(stream); stream.Close();
标签:
原文地址:http://www.cnblogs.com/hzzhao/p/5264664.html