❔ BinaryFormatter does not serialize/deserialize the Tags of listview items
so i have a listview that i want to serialize and then desirialize. I store some values in the tags of each listview item. Problem is that after i clear the listview and deserialize the file, the tags of each listview item is gone. I don't know if this is a problem of the writer or the reader, but i guess its because of the writer skipping the tag property. How can i overcome this issue?
public bool WriteToBinaryFile(string filePath, ListView listview)
{
bool result = false;
FileStream fsWrite = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
try
{
BinaryFormatter binfmt = new BinaryFormatter();
InvokeIfRequired(listview, () => { binfmt.Serialize(fsWrite, new ArrayList(listview.Items)); });
result = true;
}
catch (Exception a) { pushError(a.ToString()); result = false; }
try { fsWrite.Close(); }
catch (Exception) { }
return result;
}
public bool ReadFromBinaryFile(string filePath, ListView listview)
{
bool result = false;
FileStream fsRead = File.Open(filePath, FileMode.Open, FileAccess.Read);
try
{
// deserialize
binfmt = new BinaryFormatter();
ArrayList deserialized_array_list = (ArrayList)binfmt.Deserialize(fsRead);
Array listviewitem_array = (deserialized_array_list).ToArray(typeof(ListViewItem));
listview.Items.AddRange((ListViewItem[])listviewitem_array);
result = true;
}
catch (SerializationException) { } //exclude this exception
catch (Exception a) { pushError(a.ToString()); result = false; }
try { fsRead.Close(); }
catch (Exception) { }
return result;
} public bool WriteToBinaryFile(string filePath, ListView listview)
{
bool result = false;
FileStream fsWrite = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
try
{
BinaryFormatter binfmt = new BinaryFormatter();
InvokeIfRequired(listview, () => { binfmt.Serialize(fsWrite, new ArrayList(listview.Items)); });
result = true;
}
catch (Exception a) { pushError(a.ToString()); result = false; }
try { fsWrite.Close(); }
catch (Exception) { }
return result;
}
public bool ReadFromBinaryFile(string filePath, ListView listview)
{
bool result = false;
FileStream fsRead = File.Open(filePath, FileMode.Open, FileAccess.Read);
try
{
// deserialize
binfmt = new BinaryFormatter();
ArrayList deserialized_array_list = (ArrayList)binfmt.Deserialize(fsRead);
Array listviewitem_array = (deserialized_array_list).ToArray(typeof(ListViewItem));
listview.Items.AddRange((ListViewItem[])listviewitem_array);
result = true;
}
catch (SerializationException) { } //exclude this exception
catch (Exception a) { pushError(a.ToString()); result = false; }
try { fsRead.Close(); }
catch (Exception) { }
return result;
}