0

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();
Brad
  • 272
  • 2
  • 7
  • 22
  • Using the *using* statement so the file is always closed, even if you have an exception. You serialized the data a different way, you wrote individual *myStuff* objects, not a List<> of them. – Hans Passant Jan 12 '14 at 22:07
  • In my troubleshooting, I did try using the _using_ statement but it didn't like the fact that my list was declared outside of it. Be that as it may, is there a clean way of taking a bunch of disparate objects in a file and adding them to a list? Or should I go back and rework my code so that it is always using whole lists instead of the individual objects in terms of file read/write? – Brad Jan 12 '14 at 22:13
  • the `1 in the error means its a generic type in your case list with 1 type parameter. – terrybozzio Jan 12 '14 at 22:32

1 Answers1

1

"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);
}
realnero
  • 994
  • 6
  • 12
  • I was right, you are serializing myStuff object only. To be able to deserialize it use the code in the answer. – realnero Jan 12 '14 at 23:29
  • Correct me if I'm wrong, but your code would deserialize a single object and not all of the objects in the file, correct? How would I get to all of the objects? – Brad Jan 12 '14 at 23:48
  • Your code is putting only 1 myStuff object into File, that's where the problem is. You can change it to put list of objects into file, then you can read list back from this file. I'll update the answer – realnero Jan 12 '14 at 23:50
  • 1
    my code is only putting in one object at a time, yes. However, my current code is doing it via FileMode.Append which is adding the object at the end of the file. I will be modifying my code to just work with lists, as it appears that trying to save individual objects and then read said objects out and add them to a list just makes the pain worse. – Brad Jan 13 '14 at 00:05
  • @Brad, you are right, serialize list into file if you need to store list. – realnero Jan 13 '14 at 00:09