序列化、反序列化
主要用于存储对象状态为另一种通用格式,比如存储为二进制、xml、json等等,把对象转换成这种格式就叫序列化,而反序列化通常是从这种格式转换回来。
使用序列化主要是因为跨平台和对象存储的需求,因为网络上只允许字符串或者二进制格式,而文件需要使用二进制流格式,如果想把一个内存中的对象存储下来就必须使用序列化转换为xml(字符串)、json(字符串)或二进制(流)。
本例是二进制(流)的序列化和反序列化:
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Windows.Forms; namespace ServerManager { public partial class TimeSeting : Form { public TimeSeting() { InitializeComponent(); this.FormClosing += (sender, e) => { e.Cancel = true; this.Visible = false; }; } /// <summary> /// 序列化所保存的路径 /// </summary> private string strfile = null; public string StrFile { get { if (strfile.IsNullOrWhiteSpace()) { string path = System.Reflection.Assembly.GetExecutingAssembly().Location; strfile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), "setting.Config"); } return strfile; } set { strfile = value; } } /// <summary> /// 保存设置 并序列化数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { try { if (txtStart.Text.IsNullOrWhiteSpace() || txtEnd.Text.IsNullOrWhiteSpace() || txtMinute.Text.IsNullOrWhiteSpace() || txtFailure.Text.IsNullOrWhiteSpace()) { MessageBox.Show("设置项不能为空!","提示",MessageBoxButtons.OK); return; } MainSetting newmainset = new MainSetting(); newmainset.SetStart = Convert.ToInt32(txtStart.Text); newmainset.SetEnd = Convert.ToInt32(txtEnd.Text); newmainset.SetMinute = Convert.ToInt32(txtMinute.Text); newmainset.SetFailure = Convert.ToInt32(txtFailure.Text); if (newmainset.SetFailure < 2) { MessageBox.Show("失败间隔不能小于2分钟!"); return; } if (newmainset.SetStart > newmainset.SetEnd) { MessageBox.Show("开始时间不能小于结束时间!"); } else { MainSet = newmainset; Serialize(MainSet); this.Visible = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 序列化设置 /// </summary> /// <param name="set"></param> public void Serialize(MainSetting set) { using (FileStream fs = new FileStream(StrFile, FileMode.Create)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, set); fs.Close(); } } MainSetting mainSet; public MainSetting MainSet { get { if (mainSet == null) { DeSerialize(); } return mainSet; } set { mainSet = value; Service.HostingSystemHelper.Instance.ExecMin = value.SetStart; Service.HostingSystemHelper.Instance.ExecMax = value.SetEnd; Service.HostingSystemHelper.Instance.SetMinute = value.SetMinute; Service.HostingSystemHelper.Instance.SleepMinutes = value.SetFailure; } } /// <summary> /// 反序列化设置 /// </summary> public void DeSerialize() { if (System.IO.File.Exists(StrFile)) { using (FileStream fs = new FileStream(StrFile, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); MainSet = (MainSetting)(formatter.Deserialize(fs)); } } else { MainSet = new MainSetting() { SetStart = Service.HostingSystemHelper.Instance.ExecMin, SetEnd = Service.HostingSystemHelper.Instance.ExecMax, SetMinute = Service.HostingSystemHelper.Instance.SetMinute, SetFailure = Service.HostingSystemHelper.Instance.SleepMinutes }; } } /// <summary> /// 加载设置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TimeSeting_Load(object sender, EventArgs e) { this.txtStart.Text = MainSet.SetStart + string.Empty; this.txtMinute.Text = MainSet.SetMinute + string.Empty; this.txtEnd.Text = MainSet.SetEnd + string.Empty; this.txtFailure.Text = mainSet.SetFailure + string.Empty; } //如果输入的不是数字键,也不是回车键、Backspace键,则取消该输入 private void txtStart_KeyPress(object sender, KeyPressEventArgs e) { if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8) { e.Handled = true; } } //如果输入的不是数字键,也不是回车键、Backspace键,则取消该输入 private void txtEnd_KeyPress(object sender, KeyPressEventArgs e) { if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8) { e.Handled = true; } } //如果输入的不是数字键,也不是回车键、Backspace键,则取消该输入 private void txtMinute_KeyPress(object sender, KeyPressEventArgs e) { if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8) { e.Handled = true; } } //如果输入的不是数字键,也不是回车键、Backspace键,则取消该输入 private void txtFailure_KeyPress(object sender, KeyPressEventArgs e) { if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8) { e.Handled = true; } } } [Serializable] public class MainSetting { public int SetStart { get; set; } public int SetEnd { get; set; } public int SetMinute { get; set; } public int SetFailure { get; set; } } }
时间: 2024-10-09 16:23:28