Net序列化

--很高兴,今天终于把这个问题解决了。序列化有顺序吗?一定要先序列化在反序列化吗?(我在实际操作的时候要是首先反的话就提示xxxx流结束,反过来就不报错了。)

代码:

--实体类

[Serializable]
public class Sequence_sp
{
public string LoginName_sp { get; set; }
public DateTime LoginTime_sp { get; set; }
public string LoginIP_sp { get; set; }
}

--页面加载

protected void Page_Load(object sender, EventArgs e)
{
string fileName = Server.MapPath("~/") + "sp.txt";
Sequence_sp sq = new Sequence_sp()
{
LoginName_sp = txtName.Text,
LoginTime_sp = DateTime.Now,
LoginIP_sp = Request.UserHostAddress
};
this.Sequence(fileName, sq);

try
{
Sequence_sp sp = this.NoSequence(fileName);
LblName.Text = sp.LoginName_sp;
Lblip.Text = sp.LoginIP_sp;
LblTime.Text = sp.LoginTime_sp.ToString();
}
catch (Exception ex)
{

throw ex;
}
}

 

/// <summary>
/// 序列化
/// </summary>
/// <param name="sp">文件名</param>
/// <param name="t_sp">实体类</param>
protected void Sequence(string fileName, Sequence_sp t_sp)
{
BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
{
fs.Seek(0, SeekOrigin.Begin);
bf.Serialize(fs, t_sp);
}
}

 

/// <summary>
/// 反序列化
/// </summary>
/// <param name="t_sp"></param>
/// <returns></returns>
protected Sequence_sp NoSequence(string fileName)
{
Sequence_sp flg = null;
BinaryFormatter bf = new BinaryFormatter();

using (FileStream t_sp = new FileStream(fileName, FileMode.Open))
{
t_sp.Seek(0, SeekOrigin.Begin);
flg = bf.Deserialize(t_sp) as Sequence_sp;
}

return flg;
}

--onclick事件

string fileName = Server.MapPath("~/") + "sp.txt";
Sequence_sp sq = new Sequence_sp()
{
LoginName_sp = txtName.Text,
LoginTime_sp = DateTime.Now,
LoginIP_sp = Request.UserHostAddress
};
this.Sequence(fileName, sq);

 

--界面html

<div >
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox><br />
<asp:Button ID="BtnQuery" runat="server" Text="Button" OnClick="BtnQuery_Click" />
</div>
<div>
<span style="color: Red">姓名:</span><asp:Label runat="server" Width="155px" ID="LblName"></asp:Label><br />
<span style="color: Red">时间:</span><asp:Label ID="LblTime" Width="164px" runat="server"></asp:Label><br />
<span style="color: Red">id:</span><asp:Label ID="Lblip" Width="149px" runat="server"></asp:Label>
</div>

 

--页面传递中文乱码解决方法

System.Web.HttpUtility.UrlEncode(SPOrgan_name.Text)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值