程序在第三次编译后擦除序列化的数据
本文关键字:序列化 数据 擦除 第三次 编译 程序 | 更新日期: 2023-09-27 18:29:15
问题
启动1:本机启动,没有问题,退出时,保存当前状态
启动2:加载保存的数据,没有问题,退出时保存当前状态
启动3:由于某种原因,返回到本地启动,所有内容都处于默认位置
代码
private void Form1_Load(object sender, EventArgs e)
{
GenerateTestData(); //use method to create data upon load
DisplayEmployeeData(employees, supervisors); //use method to display data upon load
RedundancyCheck();
}
GenerateTestData Method
public void GenerateTestData()
{
Employee e1 = new Employee(MemberJob.Employee, "Name1", MemberSkills.CPlus | MemberSkills.CSharp, false);
Employee e2 = new Employee(MemberJob.Employee, "Name2", MemberSkills.CSharp | MemberSkills.Oracle | MemberSkills.CPlus, false);
Employee e3 = new Employee(MemberJob.Employee, "Name3", MemberSkills.CSharp | MemberSkills.Javascript, false);
Supervisor e4 = new Supervisor(MemberJob.Supervisor, "Name4", false);
Supervisor e5 = new Supervisor(MemberJob.Supervisor, "Name5", false);
employees.Add(e1);
employees.Add(e2);
employees.Add(e3);
supervisors.Add(e4);
supervisors.Add(e5);
}
RedundancyCheck method
private void RedundancyCheck()
{
bool _success = false;
BinaryFormatter bFormatter = new BinaryFormatter();
try
{
using (FileStream fs = new FileStream(FILENAME, FileMode.Open))
{
_success = true;
}
if (_success)
{
LoadData();
}
}
catch
{
if (!_success)
{
MessageBox.Show("There has been a problem with the main save, resorting to the backup copy!");
using (FileStream fs = new FileStream("BackupMembers.dat", FileMode.Open, FileAccess.Read))
{
List<Employee> employees = (List<Employee>)bFormatter.Deserialize(fs);
List<Supervisor> supervisors = (List<Supervisor>)bFormatter.Deserialize(fs);
ClearTable();
DisplayEmployeeData(employees, supervisors);
}
}
}
}
当表单关闭时,它运行SaveData方法
private void SaveData()
{
BinaryFormatter bFormatter = new BinaryFormatter();
using (FileStream fs = new FileStream(FILENAME, FileMode.OpenOrCreate))
{
bFormatter.Serialize(fs, employees);
bFormatter.Serialize(fs, supervisors);
}
}
例如,Name1正忙于一项工作,在我第二次加载它之后,它显示良好,然后当我第三次加载它时,它只显示TestData显示的
更新
事实证明,SaveData文件在第二次关闭时保存默认值。
第一次关闭的代码
Busy = true
EmployeeName = Name1
EmployeeWorkload = "Test"
ShiftsLeft = 2
来自第二次关闭的代码
Busy = false
EmployeeName = "Name1"
EmployeeWorkload = null
ShiftsLeft = 0
以下是的问题
using (FileStream fs = new FileStream(FILENAME, FileMode.OpenOrCreate))
FileMode.OpenOrCreate
不会截断现有文件,因此最终会得到不正确的数据。
使用FileMode.Create
,根据文件
指定操作系统应创建一个新文件。如果该文件已经存在,它将被覆盖。FileMode.Create相当于请求如果文件不存在,则使用CreateNew;否则,请使用截断。
编辑:在您的表单中,您有以下字段定义
private List<Employee> employees; //create a generic list of employees
private List<Supervisor> supervisors;
然而,在某些方法中,你会使用类似于的东西
List<Employee> employees = ...
List<Supervisor> supervisors =
它创建了一个局部变量,并且您的表单字段保持默认。要解决这个问题,请在代码中找到使用此类局部变量的位置,并使用表单变量,如
employees = ...
supervisors = ...