[.net] Saving Objects?

Started by
3 comments, last by Machaira 15 years, 10 months ago
I've searched the best I can but I'm not totally sure I know what to search for. Basic situation is this. I have a program that I use to create lots of little fun things just for me. As a part of it there are a few object types with a fairly substantial number of values in them. Nothing epic but more than I care to keep individual tabs on. So I would LIKE to save these objects just as they are into a data file so I could later read them from the HD into a Secondary program. That way I don't have to write every value individually or modify the save and load routines if I add or subtract stuff from them. I haven't had much luck looking for the solution based on not knowing what to look for and a wonky Internet connection. So is such a thing or similar thing possible? ***note my connection is down most of the time so I may not be able to answer questions right away.
------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
Advertisement
Use System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to serialize your objects.


using System.Runtime.Serialization.Formatters.Binary;using System.IO;...MemoryStream    ms = new MemoryStream   ();BinaryFormatter bf = new Binaryformatter();bf.Serialize(ms, /* This is where the instance of your object goes */);FileStream fs = new FileStream(@"C:\path\data.dat", System.IO.FileMode.Create);byte[] data = ms.ToArray();fs.Write(data, 0, data.Length);fs.Close();
Quote:Original post by Programmer One
Use System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to serialize your objects.


*** Source Snippet Removed ***


Alright. I'll see if I can't get that rolling. Thank you.

------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
Just as an afterthought, you need to mark your objects with the Serializable attribute.

[Serializable]public class MyClass{    ...}
Quote:Original post by Programmer One
Just as an afterthought, you need to mark your objects with the Serializable attribute.

Unless you inherit from something that already implements a serializable interface.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

This topic is closed to new replies.

Advertisement