准备序列化数据
[Serializable]
public class PointStatus
{
public string Key { get; set; }
public string Value { get; set; }
}
序列化 方法
IFormatter formatter = new BinaryFormatter();
string path = System.IO.Directory.GetCurrentDirectory();
path = Path.Combine(path, "data.bin");
Stream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, pointStatuses);
stream.Close();
反序列化
string path = System.IO.Directory.GetCurrentDirectory();
path = Path.Combine(path, "data.bin");
if (File.Exists(path))
{
IFormatter foramtter = new BinaryFormatter();
using (Stream destream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
List<PointStatus> pointStatuses =
(List<PointStatus>)foramtter.Deserialize(destream);
}
}