Ghi file nhị phân trong C#

Chào mọi người, mình có cú pháp ghi một List< sinhvien> ds vào trong file nhị phân vậy sai chỗ nào ạ. Khi chạy nó báo lỗi. Mong mọi người giúp đỡ ạ.

{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.bt_them = new System.Windows.Forms.Button();
            this.bt_xoa = new System.Windows.Forms.Button();
            this.bt_capnhat = new System.Windows.Forms.Button();
            this.lv_sinhvien = new System.Windows.Forms.ListView();
            this.tb_mssv = new System.Windows.Forms.TextBox();
            this.tb_hoten = new System.Windows.Forms.TextBox();
            this.tb_diachi = new System.Windows.Forms.TextBox();
            this.dt_ngaysinh = new System.Windows.Forms.DateTimePicker();
            this.bg_gioitinh = new System.Windows.Forms.GroupBox();
            this.rb_nu = new System.Windows.Forms.RadioButton();
            this.rb_Nam = new System.Windows.Forms.RadioButton();
            this.lb_hoten = new System.Windows.Forms.Label();
            this.lb_maso = new System.Windows.Forms.Label();
            this.lb_diachi = new System.Windows.Forms.Label();
            this.lb_ngaysinh = new System.Windows.Forms.Label();
            this.ch_mssv = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.ch_hoten = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.ch_gioitinh = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.ch_ngaysinh = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.ch_diachi = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.bt_ghi = new System.Windows.Forms.Button();
            this.bg_gioitinh.SuspendLayout();
            this.SuspendLayout();
            // 
            // bt_them
            // 
            this.bt_them.Location = new System.Drawing.Point(368, 42);
            this.bt_them.Name = "bt_them";
            this.bt_them.Size = new System.Drawing.Size(75, 23);
            this.bt_them.TabIndex = 6;
            this.bt_them.Text = "Thêm";
            this.bt_them.UseVisualStyleBackColor = true;
            this.bt_them.Click += new System.EventHandler(this.bt_them_Click);
            // 
            // bt_xoa
            // 
            this.bt_xoa.Location = new System.Drawing.Point(368, 83);
            this.bt_xoa.Name = "bt_xoa";
            this.bt_xoa.Size = new System.Drawing.Size(75, 23);
            this.bt_xoa.TabIndex = 7;
            this.bt_xoa.Text = "Xoá";
            this.bt_xoa.UseVisualStyleBackColor = true;
            this.bt_xoa.Click += new System.EventHandler(this.bt_xoa_Click);
            // 
            // bt_capnhat
            // 
            this.bt_capnhat.Location = new System.Drawing.Point(368, 121);
            this.bt_capnhat.Name = "bt_capnhat";
            this.bt_capnhat.Size = new System.Drawing.Size(75, 23);
            this.bt_capnhat.TabIndex = 8;
            this.bt_capnhat.Text = "Cập nhật";
            this.bt_capnhat.UseVisualStyleBackColor = true;
            this.bt_capnhat.Click += new System.EventHandler(this.bt_capnhat_Click);
            // 
            // lv_sinhvien
            // 
            this.lv_sinhvien.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.ch_mssv,
            this.ch_hoten,
            this.ch_gioitinh,
            this.ch_ngaysinh,
            this.ch_diachi});
            this.lv_sinhvien.GridLines = true;
            this.lv_sinhvien.Location = new System.Drawing.Point(68, 227);
            this.lv_sinhvien.Name = "lv_sinhvien";
            this.lv_sinhvien.Size = new System.Drawing.Size(375, 97);
            this.lv_sinhvien.TabIndex = 1;
            this.lv_sinhvien.UseCompatibleStateImageBehavior = false;
            this.lv_sinhvien.View = System.Windows.Forms.View.Details;
            this.lv_sinhvien.SelectedIndexChanged += new System.EventHandler(this.lv_sinhvien_SelectedIndexChanged);
            // 
            // tb_mssv
            // 
            this.tb_mssv.Location = new System.Drawing.Point(199, 42);
            this.tb_mssv.Name = "tb_mssv";
            this.tb_mssv.Size = new System.Drawing.Size(100, 20);
            this.tb_mssv.TabIndex = 0;
            // 
            // tb_hoten
            // 
            this.tb_hoten.Location = new System.Drawing.Point(199, 83);
            this.tb_hoten.Name = "tb_hoten";
            this.tb_hoten.Size = new System.Drawing.Size(100, 20);
            this.tb_hoten.TabIndex = 1;
            // 
            // tb_diachi
            // 
            this.tb_diachi.Location = new System.Drawing.Point(199, 164);
            this.tb_diachi.Name = "tb_diachi";
            this.tb_diachi.Size = new System.Drawing.Size(100, 20);
            this.tb_diachi.TabIndex = 4;
            // 
            // dt_ngaysinh
            // 
            this.dt_ngaysinh.Location = new System.Drawing.Point(199, 201);
            this.dt_ngaysinh.Name = "dt_ngaysinh";
            this.dt_ngaysinh.Size = new System.Drawing.Size(200, 20);
            this.dt_ngaysinh.TabIndex = 5;
            // 
            // bg_gioitinh
            // 
            this.bg_gioitinh.Controls.Add(this.rb_nu);
            this.bg_gioitinh.Controls.Add(this.rb_Nam);
            this.bg_gioitinh.Location = new System.Drawing.Point(199, 112);
            this.bg_gioitinh.Name = "bg_gioitinh";
            this.bg_gioitinh.Size = new System.Drawing.Size(145, 49);
            this.bg_gioitinh.TabIndex = 4;
            this.bg_gioitinh.TabStop = false;
            this.bg_gioitinh.Text = "Giới tính";
            // 
            // rb_nu
            // 
            this.rb_nu.AutoSize = true;
            this.rb_nu.Location = new System.Drawing.Point(97, 19);
            this.rb_nu.Name = "rb_nu";
            this.rb_nu.Size = new System.Drawing.Size(39, 17);
            this.rb_nu.TabIndex = 3;
            this.rb_nu.TabStop = true;
            this.rb_nu.Text = "Nữ";
            this.rb_nu.UseVisualStyleBackColor = true;
            // 
            // rb_Nam
            // 
            this.rb_Nam.AutoSize = true;
            this.rb_Nam.Location = new System.Drawing.Point(6, 19);
            this.rb_Nam.Name = "rb_Nam";
            this.rb_Nam.Size = new System.Drawing.Size(47, 17);
            this.rb_Nam.TabIndex = 2;
            this.rb_Nam.TabStop = true;
            this.rb_Nam.Text = "Nam";
            this.rb_Nam.UseVisualStyleBackColor = true;
            // 
            // lb_hoten
            // 
            this.lb_hoten.AutoSize = true;
            this.lb_hoten.Location = new System.Drawing.Point(118, 93);
            this.lb_hoten.Name = "lb_hoten";
            this.lb_hoten.Size = new System.Drawing.Size(39, 13);
            this.lb_hoten.TabIndex = 5;
            this.lb_hoten.Text = "Họ tên";
            // 
            // lb_maso
            // 
            this.lb_maso.AutoSize = true;
            this.lb_maso.Location = new System.Drawing.Point(118, 52);
            this.lb_maso.Name = "lb_maso";
            this.lb_maso.Size = new System.Drawing.Size(37, 13);
            this.lb_maso.TabIndex = 5;
            this.lb_maso.Text = "MSSV";
            // 
            // lb_diachi
            // 
            this.lb_diachi.AutoSize = true;
            this.lb_diachi.Location = new System.Drawing.Point(118, 167);
            this.lb_diachi.Name = "lb_diachi";
            this.lb_diachi.Size = new System.Drawing.Size(40, 13);
            this.lb_diachi.TabIndex = 5;
            this.lb_diachi.Text = "Địa chỉ";
            // 
            // lb_ngaysinh
            // 
            this.lb_ngaysinh.AutoSize = true;
            this.lb_ngaysinh.Location = new System.Drawing.Point(118, 207);
            this.lb_ngaysinh.Name = "lb_ngaysinh";
            this.lb_ngaysinh.Size = new System.Drawing.Size(54, 13);
            this.lb_ngaysinh.TabIndex = 5;
            this.lb_ngaysinh.Text = "Ngày sinh";
            // 
            // ch_mssv
            // 
            this.ch_mssv.Text = "MSSV";
            this.ch_mssv.Width = 61;
            // 
            // ch_hoten
            // 
            this.ch_hoten.Text = "Họ tên";
            this.ch_hoten.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.ch_hoten.Width = 80;
            // 
            // ch_gioitinh
            // 
            this.ch_gioitinh.Text = "Giới tính";
            this.ch_gioitinh.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.ch_gioitinh.Width = 62;
            // 
            // ch_ngaysinh
            // 
            this.ch_ngaysinh.Text = "Ngày sinh";
            this.ch_ngaysinh.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.ch_ngaysinh.Width = 75;
            // 
            // ch_diachi
            // 
            this.ch_diachi.Text = "Địa chỉ";
            this.ch_diachi.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // bt_ghi
            // 
            this.bt_ghi.Location = new System.Drawing.Point(368, 167);
            this.bt_ghi.Name = "bt_ghi";
            this.bt_ghi.Size = new System.Drawing.Size(75, 23);
            this.bt_ghi.TabIndex = 9;
            this.bt_ghi.Text = "Ghi";
            this.bt_ghi.UseVisualStyleBackColor = true;
            this.bt_ghi.Click += new System.EventHandler(this.bt_ghi_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(503, 326);
            this.Controls.Add(this.bt_ghi);
            this.Controls.Add(this.lb_ngaysinh);
            this.Controls.Add(this.lb_diachi);
            this.Controls.Add(this.lb_maso);
            this.Controls.Add(this.lb_hoten);
            this.Controls.Add(this.bg_gioitinh);
            this.Controls.Add(this.dt_ngaysinh);
            this.Controls.Add(this.tb_diachi);
            this.Controls.Add(this.tb_hoten);
            this.Controls.Add(this.tb_mssv);
            this.Controls.Add(this.lv_sinhvien);
            this.Controls.Add(this.bt_capnhat);
            this.Controls.Add(this.bt_xoa);
            this.Controls.Add(this.bt_them);
            this.Name = "Form1";
            this.Text = "Form1";
            this.bg_gioitinh.ResumeLayout(false);
            this.bg_gioitinh.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button bt_them;
        private System.Windows.Forms.Button bt_xoa;
        private System.Windows.Forms.Button bt_capnhat;
        private System.Windows.Forms.ListView lv_sinhvien;
        private System.Windows.Forms.TextBox tb_mssv;
        private System.Windows.Forms.TextBox tb_hoten;
        private System.Windows.Forms.TextBox tb_diachi;
        private System.Windows.Forms.DateTimePicker dt_ngaysinh;
        private System.Windows.Forms.GroupBox bg_gioitinh;
        private System.Windows.Forms.RadioButton rb_nu;
        private System.Windows.Forms.RadioButton rb_Nam;
        private System.Windows.Forms.Label lb_hoten;
        private System.Windows.Forms.Label lb_maso;
        private System.Windows.Forms.Label lb_diachi;
        private System.Windows.Forms.Label lb_ngaysinh;
        private System.Windows.Forms.ColumnHeader ch_mssv;
        private System.Windows.Forms.ColumnHeader ch_hoten;
        private System.Windows.Forms.ColumnHeader ch_gioitinh;
        private System.Windows.Forms.ColumnHeader ch_ngaysinh;
        private System.Windows.Forms.ColumnHeader ch_diachi;
        private System.Windows.Forms.Button bt_ghi;
    }
}

```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;
using System.IO;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;

namespace CT_SinhVien
{
    public partial class Form1 : Form
    {
        private List<SinhVien> ds;
        public Form1()
        {
            InitializeComponent();
            ds = new List<SinhVien>();
        }
        //Them 1 ong sv vào items trong listview
        private ListViewItem ThemThongTinSV(SinhVien sv)
        {
            ListViewItem lvi = new ListViewItem(sv.Maso);
            lvi.SubItems.Add(sv.Hoten);
            lvi.SubItems.Add(sv.Diachi);
            lvi.SubItems.Add(sv.Ngaysinh.ToShortDateString());
            lvi.SubItems.Add(sv.Gioitinh ? "Nam" : "Nữ");
            return lvi;
        }
        private void HienThi()
        {
            lv_sinhvien.Items.Clear();
            foreach(SinhVien sv in ds)
            {
                ListViewItem lvi = ThemThongTinSV(sv);
                lv_sinhvien.Items.Add(lvi);
            }
        }
        private void Clear()
        {
            tb_mssv.Clear();
            tb_hoten.Clear();
            tb_diachi.Clear();
            tb_mssv.Focus();
        }
        private void bt_them_Click(object sender, EventArgs e)
        {
            SinhVien sv = new SinhVien(tb_mssv.Text, tb_hoten.Text, tb_diachi.Text, dt_ngaysinh.Value, rb_Nam.Checked);
            ds.Add(sv);
            HienThi();
            Clear();
        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            foreach(ListViewItem lvi in lv_sinhvien.SelectedItems)
            {
                foreach(SinhVien sv in ds)
                {
                    if(sv.Maso.Equals(lvi.SubItems[0].Text))
                    {
                        ds.Remove(sv);
                        break;
                    }
                }
            }
            HienThi();
        }

        private void HienThiTTLen(SinhVien sv)
        {
            tb_mssv.Text = sv.Maso;
            tb_hoten.Text = sv.Hoten;
            tb_diachi.Text = sv.Diachi;
            dt_ngaysinh.Value = sv.Ngaysinh;
            rb_Nam.Checked = sv.Gioitinh;
        }
        private void bt_capnhat_Click(object sender, EventArgs e)
        {
            foreach(SinhVien sv in ds)
            {
                if(sv.Maso.Equals(tb_mssv.Text))
                {
                    sv.Hoten = tb_hoten.Text;
                    sv.Diachi = tb_diachi.Text;
                    sv.Ngaysinh = dt_ngaysinh.Value;
                    sv.Gioitinh = rb_Nam.Checked;
                }
            }
            HienThi();
        }

        private void lv_sinhvien_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lv_sinhvien.SelectedItems)
            {
                foreach (SinhVien sv in ds)
                {
                    if (sv.Maso.Equals(lvi.SubItems[0].Text))
                    {
                        HienThiTTLen(sv);
                        break;
                    }
                }
            }
        }

        private void bt_ghi_Click(object sender, EventArgs e)
        {
            FileStream f = new FileStream("hocsinh.dat", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(f, ds);
            f.Close();
        }

        
    }
}

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

namespace CT_SinhVien
{
    class SinhVien
    {
        private string v_maso;
        private string v_hoten;
        private string v_diachi;
        private bool v_gioitinh;
        private DateTime v_ngaysinh;
        public string Maso
        {
            get { return v_maso; }
            set { v_maso = value; }
        }
        public string Hoten
        {
            get { return v_hoten; }
            set { v_hoten = value; }
        }
        public string Diachi
        {
            get { return v_diachi; }
            set { v_diachi = value; }
        }
        public bool Gioitinh
        {
            get { return v_gioitinh; }
            set { v_gioitinh = value; }
        }
        public DateTime Ngaysinh
        {
            get { return v_ngaysinh; }
            set { v_ngaysinh = value; }
        }
        public SinhVien()
        {
            Maso = "";
            Hoten = "";
            Diachi = "";
            Gioitinh = true;
            Ngaysinh = DateTime.Now;
        }
        public SinhVien(string maso, string hoten, string diachi, DateTime ngaysinh, bool gioitinh)
        {
            Maso = maso;
            Hoten =hoten;
            Diachi = diachi;
            Gioitinh = gioitinh;
            Ngaysinh = ngaysinh;
        }
    }
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CT_SinhVien
{
    class SinhVien
    {
        private string v_maso;
        private string v_hoten;
        private string v_diachi;
        private bool v_gioitinh;
        private DateTime v_ngaysinh;
        public string Maso
        {
            get { return v_maso; }
            set { v_maso = value; }
        }
        public string Hoten
        {
            get { return v_hoten; }
            set { v_hoten = value; }
        }
        public string Diachi
        {
            get { return v_diachi; }
            set { v_diachi = value; }
        }
        public bool Gioitinh
        {
            get { return v_gioitinh; }
            set { v_gioitinh = value; }
        }
        public DateTime Ngaysinh
        {
            get { return v_ngaysinh; }
            set { v_ngaysinh = value; }
        }
        public SinhVien()
        {
            Maso = "";
            Hoten = "";
            Diachi = "";
            Gioitinh = true;
            Ngaysinh = DateTime.Now;
        }
        public SinhVien(string maso, string hoten, string diachi, DateTime ngaysinh, bool gioitinh)
        {
            Maso = maso;
            Hoten =hoten;
            Diachi = diachi;
            Gioitinh = gioitinh;
            Ngaysinh = ngaysinh;
        }
    }
}
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;
using System.IO;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;

namespace CT_SinhVien
{
    public partial class Form1 : Form
    {
        private List<SinhVien> ds;
        public Form1()
        {
            InitializeComponent();
            ds = new List<SinhVien>();
        }
        //Them 1 ong sv vào items trong listview
        private ListViewItem ThemThongTinSV(SinhVien sv)
        {
            ListViewItem lvi = new ListViewItem(sv.Maso);
            lvi.SubItems.Add(sv.Hoten);
            lvi.SubItems.Add(sv.Diachi);
            lvi.SubItems.Add(sv.Ngaysinh.ToShortDateString());
            lvi.SubItems.Add(sv.Gioitinh ? "Nam" : "Nữ");
            return lvi;
        }
        private void HienThi()
        {
            lv_sinhvien.Items.Clear();
            foreach(SinhVien sv in ds)
            {
                ListViewItem lvi = ThemThongTinSV(sv);
                lv_sinhvien.Items.Add(lvi);
            }
        }
        private void Clear()
        {
            tb_mssv.Clear();
            tb_hoten.Clear();
            tb_diachi.Clear();
            tb_mssv.Focus();
        }
        private void bt_them_Click(object sender, EventArgs e)
        {
            SinhVien sv = new SinhVien(tb_mssv.Text, tb_hoten.Text, tb_diachi.Text, dt_ngaysinh.Value, rb_Nam.Checked);
            ds.Add(sv);
            HienThi();
            Clear();
        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            foreach(ListViewItem lvi in lv_sinhvien.SelectedItems)
            {
                foreach(SinhVien sv in ds)
                {
                    if(sv.Maso.Equals(lvi.SubItems[0].Text))
                    {
                        ds.Remove(sv);
                        break;
                    }
                }
            }
            HienThi();
        }

        private void HienThiTTLen(SinhVien sv)
        {
            tb_mssv.Text = sv.Maso;
            tb_hoten.Text = sv.Hoten;
            tb_diachi.Text = sv.Diachi;
            dt_ngaysinh.Value = sv.Ngaysinh;
            rb_Nam.Checked = sv.Gioitinh;
        }
        private void bt_capnhat_Click(object sender, EventArgs e)
        {
            foreach(SinhVien sv in ds)
            {
                if(sv.Maso.Equals(tb_mssv.Text))
                {
                    sv.Hoten = tb_hoten.Text;
                    sv.Diachi = tb_diachi.Text;
                    sv.Ngaysinh = dt_ngaysinh.Value;
                    sv.Gioitinh = rb_Nam.Checked;
                }
            }
            HienThi();
        }

        private void lv_sinhvien_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lv_sinhvien.SelectedItems)
            {
                foreach (SinhVien sv in ds)
                {
                    if (sv.Maso.Equals(lvi.SubItems[0].Text))
                    {
                        HienThiTTLen(sv);
                        break;
                    }
                }
            }
        }

        private void bt_ghi_Click(object sender, EventArgs e)
        {
            FileStream f = new FileStream("hocsinh.dat", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(f, ds);
            f.Close();
        }

        
    }
}

Các class/struct tham gia vào việc Serialization đều phải có attribute Serializable trên đầu.

[Serializable]
class SinhVien{
    //...
}
2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?