星空网 > 软件开发 > ASP.net

序列化和反序列化简单使用

序列化、反序列化
 
     主要用于存储对象状态为另一种通用格式,比如存储为二进制、
  使用序列化主要是因为跨平台和对象存储的需求,因为网络上只允许字符串或者二进制格式,而文件需要使用二进制流格式,如果想把一个内存中的对象存储下来就必须使用序列化转换为 
  本例是二进制(流)的序列化和反序列化:
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;    }  }}

 





原标题:序列化和反序列化简单使用

关键词:序列

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

外贸提单是什么?你需要知道的那些基本知识和注意点!:https://www.kjdsnews.com/a/1381828.html
赶紧冲!亚马逊美国站这五类产品被点名!:https://www.kjdsnews.com/a/1381829.html
深圳一服务商被抓,卖账号月赚60万!:https://www.kjdsnews.com/a/1381830.html
转型险些死掉,大卖:不必非做精品:https://www.kjdsnews.com/a/1381831.html
一张床垫13万,出海20多国!慕思如何打造国际范儿?:https://www.kjdsnews.com/a/1381832.html
品类趋势:时尚、美妆、3C科技在TikTok有哪些新商机?:https://www.kjdsnews.com/a/1381833.html
从园岭新村到大梅沙海滨总站坐什么车:https://www.vstour.cn/a/363191.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流