Search code examples
c#winformsbinary-serialization

C# file won't save


Saving or reading a file, what is wrong?

This creates empty files.

I'm confused, please tell me how to do it properly. As you can see I'm trying to save a class and then read an array of them back.

public void savePlayers()
{
    string path = @"scores.dat";

    if (File.Exists(path))
    {
        File.Delete(path);
    }

    try
    {
        using (FileStream fs = File.Create(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(fs, player.players);
            fs.Close();
        }
    }
    catch
    {
        MessageBox.Show("Failed to save data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

public void readPlayers()
{
    string path = @"scores.dat";

    player.players.Clear();
    try
    {
        using (FileStream fs = File.OpenRead(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            player.players.Add((Player)formatter.Deserialize(fs));
            fs.Close();
        }
    }
    catch
    {
        MessageBox.Show("Failed to read stats file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Solution

  • You are saving a player.players collection and you are trying to load a single Player (player.players.Add((Player)formatter.Deserialize(fs));). This is not correct.

    It depends where you have to fix this on the loading side (Deserialize) or at the saving side (Serialize).

    // Saving
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(fs, player.players.Count); // or Count(), Length, depends on your list, collection,...
    for each (Player pl in player.players)
    {
      formatter.Serialize(fs, pl);
    }
    fs.Close();
    
    // Loading
    BinaryFormatter formatter = new BinaryFormatter();
    int count = (Int32) formatter.Deserialize(fs);
    for (int i = 0; i < count; i++)
    {
      player.players.Add((Player)formatter.Deserialize(fs));
    }
    fs.Close();
    

    And Player class has to be marked as [Serializable], please check if it has this attribute.