I am using serialization to write with Append some contacts with their name and phone to a file, I am writing each contact alone by append it to the file.
I want to read these contacts from the file and put them in a ListView.
Stream stream = File.Open("Contacts.dat", FileMode.Open);
BinaryFormatter BFormatter = new BinaryFormatter();
Contacts contact = (Contacts)BFormatter.Deserialize(stream);
listView1.Items.Add(new ListViewItem(new string[] { "", contact.name, contact.phone }));
After I am doing the above code I get only one contact and if I deserialize the stream again i get the next contact
Contacts contact2 = (Contacts)BFormatter.Deserialize(stream);
listView1.Items.Add(new ListViewItem(new string[] { "", contact2.name, contact2.phone}));
How can I read all the contacts?
or how can I know that the stream is end?
You can check the stream
Positionproperty against streamLengthproperty, assuming your are refering to file stream