writing object to file system

  • Thread starter Thread starter Alex Wagner
  • Start date Start date
A

Alex Wagner

I know it must be a very simple thing to do but I'm just brain dead today...
sorry...

how can I save an object to the filesystem??

object obj = GetMyObject();

// insert code to save to file system...

Please HELP...

thanks.

Alex
 
Alex,

If the object is marked with the Serializable attribute, then you can
create a BinaryFormatter and then serialize it to a filestream, like so:

// Create a file stream.
using (FileStream fileStream = new FileStream(@"c:\temp\obj.bin",
FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
// Create a formatter.
IFormatter formatter = new BinaryFormatter();

// Serialize.
formatter.Serialize(fileStream, obj);
}

Hope this helps.
 
Back
Top