--很高兴,今天终于把这个问题解决了。序列化有顺序吗?一定要先序列化在反序列化吗?(我在实际操作的时候要是首先反的话就提示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)