0

I'm trying to save multiple object that the user create to a binary file. So far I am able to create a binary file of one object.

 public class BinSerializerUtility
    {

        public 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();
            }

        }

MainForm:

private void SaveToFile(string filename)
        {
            for (int index = 0; index < animalmgr.Count; index++)
            {
                Animal animal = animalmgr.GetAt(index);
                BinSerializerUtility BinSerial = new BinSerializerUtility();
                BinSerial.BinaryFileSerialize(animal, filename);

            }
        }

private void mnuFileSaveAs_Click(object sender, EventArgs e)
        {
            //Show save-dialogbox
            if(saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string thefilename = saveFileDialog1.FileName;
                SaveToFile(thefilename);
            }
        }

I'm not really sure how to make it so it can save multiple objects to binary file. Do you have any tips?

I did try the following:

public byte[] SerializeArray(object obj)
        {
            byte[] serializedObject = null;
            MemoryStream memStream = null;

            try
            {
                memStream = new MemoryStream();
                BinaryFormatter binFormatter = new BinaryFormatter();

                binFormatter.Serialize(memStream, obj);
                memStream.Seek(0, 0);               //set position at 0,0
                serializedObject = memStream.ToArray();
            }
            finally
            {
                if (memStream != null)
                    memStream.Close();
            }

            return serializedObject;    // return the array.
        }

But the problem with it is that I don't know where to insert the fileName (The path)

1 Answer 1

1

You can modify BinaryFileSerialize to accept an array:

public void BinaryFileSerialize(object [] objs, string filePath). Then you can loop over that array to insert each item in the array:

FileStream fileStream = new FileStream(filePath, FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
foreach(var obj in objs) {
    b.Serialize(fileStream, obj);
}

SaveToFile function:

private void SaveToFile(string filename)
{
    //Animal array
    Animal [] animals = new Animal[animalmgr.Count];
    for (int index = 0; index < animalmgr.Count; index++)
    {
        animals[index] = animalmgr.GetAt(index);
    }
    BinSerializerUtility BinSerial = new BinSerializerUtility();
    BinSerial.BinaryFileSerialize(animals, filename);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.