我打算做个管理学生成绩的小程序,数据存储打算用序列化,BinaryFormatter。建个学生类,把每个类对象存到一个二进制文件中去,但是我每次只能存一个对象啊,存了一个新的对象,上一个对象就没了,每个二进制文件里只有一个对象,怎么办可以让一个二进制文件中存很多个对象? Student stu = new Student();
ArrayList writebin = new ArrayList();
FileStream fs = new FileStream("temp.bin", FileMode.Open, FileAccess.Write);
BinaryFormatter bf=new BinaryFormatter();
Form1 f1 = new Form1();
public form2()
{
InitializeComponent();
} private void ok_Click(object sender, EventArgs e)
{
stu.StudID = id.Text.ToString();
stu.StudName = name.Text.ToString();
stu.StudBirthday = birthday.Text.ToString();
stu.StudSexMale = sexmale.Text.ToString();
stu.chinese = int.Parse(chinese.Text);
stu.math = int.Parse(math.Text);
stu.englise =int.Parse( english.Text);
writebin.Add(stu);
bf.Serialize(fs,writebin );
fs.Close();
ArrayList writebin = new ArrayList();
FileStream fs = new FileStream("temp.bin", FileMode.Open, FileAccess.Write);
BinaryFormatter bf=new BinaryFormatter();
Form1 f1 = new Form1();
public form2()
{
InitializeComponent();
} private void ok_Click(object sender, EventArgs e)
{
stu.StudID = id.Text.ToString();
stu.StudName = name.Text.ToString();
stu.StudBirthday = birthday.Text.ToString();
stu.StudSexMale = sexmale.Text.ToString();
stu.chinese = int.Parse(chinese.Text);
stu.math = int.Parse(math.Text);
stu.englise =int.Parse( english.Text);
writebin.Add(stu);
bf.Serialize(fs,writebin );
fs.Close();
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货