Brad
Brad

Reputation: 272

Pulling objects from binary file and putting in List<T>

I've done this before, but can't figure out how I did it. I have a binary file that I want to store objects in. I've serialized the object class with [Serializable] and have successfully saved to the file individual objects to it. However, problems start showing up when trying to read the objects out and placing in a List<> for further handling.

So when trying to just read the objects out of the file, I have the code:

List<myStuff> aListOfStuff = new List<myStuff>();
Stream fileStream = File.OpenRead("MyFile.DAT");
BinaryFormatter deserializer = new BinaryFormatter();
aListOfStuff = (List<myStuff>)deserializer.Deserialize(fileStream);
fileStream.Close();

When I first started testing, no errors were generated on the above code, but I would receive an exception when trying to write to the file that the file couldn't be accessed because it was being used by another process. Upon trying to troubleshoot by commenting out all sections of code that would try to check the file for already-existing contents and only having the save & refresh code active, I would receive an error on the aListofStuff line of code above that stated "Unable to cast object of type 'myStuff' to type 'System.Collections.Generic.List'1. Yes, the 1 is in the error and not a typo. This tells me that the reason I was getting the 'file still open' message is because the .Close() wasn't being called but that still doesn't help me get to the bottom of the problem.

So I'm assuming I just don't know/remember how to pull a list of objects back out of a file. I've searched the web for how to do this and can't find information regarding specifically to lists of objects. Plenty of stuff for a single serialized object in a file, but not more than one and those articles that I've found for serializing don't seem to scale.

Here's the code I'm using to add an object to the file:

myStuff newThing = new myStuff(string1, string2, string3);
Stream fileStream = File.Open("MyFile.Dat", FileMode.Append);
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(fileStream, newThing);
fileStream.Close();

Upvotes: 0

Views: 2386

Answers (1)

realnero
realnero

Reputation: 994

"Unable to cast object of type 'myStuff' to type 'System.Collections.Generic.List'1.

This means that you serialized into File one myStuff object, not a list of objects. Check this. Cna you also provide serialization code?

UPDATE2:

To serialize:

var list = new List<myStuff>();
var objectOne = new myStuff(string1, string2, string3);
// more object here
list.Add(objectOne);
// list.Add other objects there

using(Stream fileStream = File.Open("MyFile.Dat", FileMode.Create))
{
  BinaryFormatter serializer = new BinaryFormatter();
  serializer.Serialize(fileStream, list);
}

To deserialize:

List<myStuff> list;
using(Stream fileStream = File.OpenRead("MyFile.DAT"))
{
  BinaryFormatter deserializer = new BinaryFormatter();
  list = (myStuffList<myStuff>)deserializer.Deserialize(fileStream);
}

Upvotes: 1

Related Questions