把对象保存在文件中(格式为二进制文件):
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();
时间: 2024-10-05 04:49:05