点滴积累【C#】---序列化和反序列化_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 点滴积累【C#】---序列化和反序列化

点滴积累【C#】---序列化和反序列化

 2013/11/23 3:26:30  青苹果  博客园  我要评论(0)
  • 摘要:序列化和反序列化效果图:序列化和反序列化代码:需要添加两个命名空间:usingSystem.IO;usingSystem.Runtime.Serialization.Formatters.Binary;List<Game>allgame=newList<Game>();privatevoidbutton1_Click(objectsender,EventArgse){stringname=textBox1.Text.Trim();stringtype=textBox2
  • 标签:C# 积累 序列化

序列化和反序列化效果图:

序列化和反序列化代码:

需要添加两个命名空间:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

List<Game> allgame = new List<Game>();
        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text.Trim();
            string type = textBox2.Text.Trim();
            string time = textBox3.Text.Trim();
            Game gm = new Game(name, type, time);
            allgame.Add(gm);
            MessageBox.Show("添加成功");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("game.bin", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, allgame);
            fs.Close();
            MessageBox.Show("序列化成功");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form1 from = new Form1();
            from.ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //反序列化
            FileStream fs = new FileStream("game.bin", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            List<Game> gm = (List<Game>)bf.Deserialize(fs);
            fs.Close();

            foreach (Game game in gm)
            {
                textBox4.Text += game.Name + " " + game.Type + " " + game.Time;
            }
        }

 

发表评论
用户名: 匿名