❔ 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;
}
5 Replies
Klarth
Klarth2y ago
You shouldn't use BinaryFormatter anymore because of security reasons https://learn.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide You also shouldn't use GUI controls as your data store. ListView supports binding through DataSource, although it's not as capable as modern frameworks. Normally, I create persistence/serialization models. That is a class that contains all relevant properties that get serialized. Then you map the data between the Model and ListViewItem. And you serialize/deserialize with System.Text.Json / NewtonSoft / System.Xml. You may be able to skip the mapping step if you use data binding via DataSource. You can in XAML-based languages, but WinForms is not very good, especially in that area.
Diesel Geezer
Diesel Geezer2y ago
thanks for the other solutions, but what about the BinaryFormatter? Do you know how i can achieve what i asked?
Klarth
Klarth2y ago
I don't help users write obviously bad code.
Diesel Geezer
Diesel Geezer2y ago
alright...
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts