Okay, so I'm trying to save ListView with its data through serialization. Using these two methods:
public static T BinaryFileDeserialize<T>(string filePath)
{
FileStream fileStream = null;
Object obj;
try
{
if (!File.Exists(filePath))
throw new FileNotFoundException("The file" + "was not found.", filePath);
fileStream = new FileStream(filePath, FileMode.Open);
BinaryFormatter b = new BinaryFormatter();
obj = b.Deserialize(fileStream);
}
catch
{
throw;
}
finally
{
if (fileStream != null)
fileStream.Close();
}
return (T)obj;
}
and
public static void BinaryFileSerialize(Object obj, string filePath)
{
FileStream fileStream = null;
try
{
fileStream = new FileStream(filePath, FileMode.Create);
BinaryFormatter b =
new BinaryFormatter();
b.Serialize(fileStream, obj);
}
catch
{
throw;
}
finally
{
if (fileStream != null)
fileStream.Close();
}
}
Also, since I didn't include or write the methods to fit only this particular purpose of saving ListViews I'm trying - ye, that's correct, trying - to use these two in addition, like helpers:
public ArrayList SaveListView(ListView LV)
{
ArrayList alSavedLV = new ArrayList();
for (int i = 0; (i <= animalList.Count - 1); i++)
{
alSavedLV.Add(LV.Items[i]);
}
return alSavedLV;
}
public ListView LoadListView(ArrayList AL)
{
ListView lv = new ListView();
for (int i = 0; (i <= (AL.Count - 1)); i++)
{
ListViewItem lvi = new ListViewItem();
lvi = ((ListViewItem)(AL[i]));
lv.Items.Add(lvi);
}
return lv;
}
However, as far as saving goes, I think it works. The deserialization part on the other hand doesn't come through, or at least it doesn't fill my ListView with the saved data. Any ideas? Where might I be going wrong?
Happy easter!