1. 程式人生 > 其它 >C# 二進位制序列化和返序列化儲存和讀取物件資訊

C# 二進位制序列化和返序列化儲存和讀取物件資訊

直接用明文文字的方式儲存物件資訊不是非常科學,現在用二進位制序列化的方式來儲存。

這裡要先增加名稱空間:using System.Runtime.Serialization.Formatters.Binary;,還要給實體類增加序列化標識特性。

通過分層設計來實現:

實體類

Student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SaveClassInfoToFile
{
    [Serializable]  // 增加序列化特性
    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
        public string Brithday { get; set; }
    }
}

Service

StudentService.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace SaveClassInfoToFile
{
    class StudentService
    {
        public Student stu;

        // 序列化
        public void SerializeObj()
        {
            // [1]建立檔案流
            FileStream fs = new FileStream(@"stuInfo.stu", FileMode.Create);
            // [2]建立二進位制格式
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            // [3]呼叫序列化方法
            binaryFormatter.Serialize(fs, stu);
            // [4]關閉檔案流
            fs.Close();
        }
        // 返序列化
        public Student DeserializeObj()
        {
            // 和序列化的步驟類似
            FileStream fs = new FileStream(@"stuInfo.stu", FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();
            Student student = formatter.Deserialize(fs) as Student;
            fs.Close();

            return student;
        }
    }
}

介面

Form.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SaveClassInfoToFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        
        // 序列化儲存物件資訊
        private void button4_Click(object sender, EventArgs e)
        {
            Student stu = new Student() {
                Name = this.txtName.Text.Trim(),
                Age = int.Parse(this.txtAge.Text.Trim()),
                Gender = this.txtGender.Text.Trim(),
                Brithday = this.txtBrith.Text.Trim()
            };

            StudentService stuService = new StudentService() { stu = stu};
            stuService.SerializeObj();  // 呼叫序列化方法
        }
        // 返序列化還原物件資訊
        private void button3_Click(object sender, EventArgs e)
        {
            StudentService studentService = new StudentService();
            Student stu = studentService.DeserializeObj();

            // 把資料讀出來
            this.txtAge.Text = stu.Age.ToString();
            this.txtGender.Text = stu.Gender;
            this.txtName.Text = stu.Name;
            this.txtBrith.Text = stu.Brithday;
        }
    }
}

執行效果