I save a List of my class into a binary file and use FileStream and BinaryFormatter.
private void SaveCustomers()
{
FileStream fs = null;
try
{
fs = new FileStream( Application.StartupPath + dataPath + @"\" + customersFilename, FileMode.Create, FileAccess.Write );
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, customers);
}
catch (Exception ex)
{
MessageBox.Show( ex.Message, "Fehler beim speichern der Besucherdaten", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
if (fs != null)
{
fs.Flush();
fs.Close();
}
}
}
At some point in my programm the file gets "destroyed". Since this is the only methode with the right to write in the file I think this methode is the problem.
My assumption is that the BinaryFormatter is not finished when the Filestream gets flushed and closed.
It only happend lately since the file is around 8 MB at the beginning it worked flawless.
Am I right in my assumption? or is it completly different.
private void LoadCustomers()
{
FileStream fs = null;
try
{
fs = new FileStream( Application.StartupPath + dataPath + @"\" + customersFilename, FileMode.OpenOrCreate, FileAccess.Read );
BinaryFormatter bf = new BinaryFormatter();
customers = (List<Customer>)bf.Deserialize( fs );
if (customers == null) customers = new List<Customer>();
}
catch (Exception ex)
{
MessageBox.Show( ex.Message, "Fehler beim laden der Besucherdaten", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
if (fs != null)
{
fs.Flush();
fs.Close();
}
}
}
The last code is my reader.