Jump to content
  • Advertisement
Sign in to follow this  
Goober King

[.net] Saving Objects?

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!