分享一个序列化反序列化保存对象的一个代码示例
这串代码是把用户信息序列化保存到本地并且再取出
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace C4Mail
{
public class MailInfoIoLocal
{
/// <summary>
/// 保存发件人信息到本地
/// </summary>
/// <param name="localmailinfo"></param>
public void Save(LocalMailInfo localmailinfo)
{
FileStream stream = new FileStream("MailInfo/MailInfo.Bin", FileMode.Create);
BinaryFormatter format = new BinaryFormatter();
format.Serialize(stream, localmailinfo);
stream.Close();
}
/// <summary>
/// 取出本地的发件人信息
/// </summary>
/// <returns></returns>
public LocalMailInfo Take()
{
FileStream stream = new FileStream("MailInfo/MailInfo.Bin", FileMode.Open);
BinaryFormatter format = new BinaryFormatter();
LocalMailInfo localmailinfo = (LocalMailInfo)format.Deserialize(stream);
stream.Close();
return localmailinfo;
}
}
}
代码仅供参考,其他还得靠自己
|