Now before we start, please don't send me off on XML Serialization, I want binary for a reason. :)
In the following code (and I don't even know if its correct ... it does compile), I'm trying to serialize and write a list of my class to a file and then read it back
I'm understanding everything until we get to the reading part ... every fricking example on the internet writes just one record and reads it back.
Down at line 35 ... I'm not sure how write the loop to tell if I've reached the end of my file or if I even need to be concerned with it because serialization does the entire file ... but if it does the entire file, then supplying just record isn't going to cut it.
public void WriteRecordsToFile()
{
//Create the file name ...
string fn = Properties.Settings.Default.ExecutionPath + "IIVRecords.DAT";
Stream stream = File.Open(fn, FileMode.Create);
//Make the formatter ...
BinaryFormatter bformatter = new BinaryFormatter();
if (reclist.Count > 0)
{
for (int x = 0; x < reclist.Count; x++)
{
record = reclist[x];
bformatter.Serialize(stream, record);
}
}
stream.Close();
}
public void ReadRecordsFromFile()
{
record = null;
reclist.Clear();
//create the filename path
string fn = Properties.Settings.Default.ExecutionPath + "IIVRecords.DAT";
//Open the file written above and read values from it.
stream = File.Open(fn, FileMode.Open);
bformatter = new BinaryFormatter();
record = (RecordClass)bformatter.Deserialize(stream);
stream.Close();
}
Any help, improvements, comments are appreciated.