Coder Perfect

MemoryStream can be saved and loaded from a file.

Problem

I’m trying to save and load a serialized structure that I’ve serialized into a MemoryStream.

So, how do you save a MemoryStream to a file and then get it from there?

Asked by Mahdi Ghiasi

Solution #1

MemoryStream is an option. To write the content of a memory stream to another stream, use the WriteTo or Stream.CopyTo methods (available in framework versions 4.5.2, 4.5.1, 4.5, 4) methods.

memoryStream.WriteTo(fileStream);

Update:

fileStream.CopyTo(memoryStream);
memoryStream.CopyTo(fileStream);

Answered by KV Prajapati

Solution #2

Assume the name of the MemoryStream is ms.

MemoryStream is written to a file with this code:

using (FileStream file = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.Write)) {
   byte[] bytes = new byte[ms.Length];
   ms.Read(bytes, 0, (int)ms.Length);
   file.Write(bytes, 0, bytes.Length);
   ms.Close();
}

This reads a file and saves it to a MemoryStream:

using (MemoryStream ms = new MemoryStream())
using (FileStream file = new FileStream("file.bin", FileMode.Open, FileAccess.Read)) {
   byte[] bytes = new byte[file.Length];
   file.Read(bytes, 0, (int)file.Length);
   ms.Write(bytes, 0, (int)file.Length);
}

You may copy FileStream to MemoryStream and back in.Net Framework 4+ as easily as this:

MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream("file.bin", FileMode.Open, FileAccess.Read))
    file.CopyTo(ms);

In the other direction (from MemoryStream to FileStream):

using (FileStream file = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.Write))
    ms.CopyTo(file);

Answered by Ashkan Mobayen Khiabani

Solution #3

Even if there is an exception (usually on file I/O), the stream should be disposed away – my preferred method for this is to utilize clauses, so for writing your MemoryStream, you can use:

using (FileStream file = new FileStream("file.bin", FileMode.Create, FileAccess.Write)) {
    memoryStream.WriteTo(file);
}

And here’s how to read it back:

using (FileStream file = new FileStream("file.bin", FileMode.Open, FileAccess.Read)) {
    byte[] bytes = new byte[file.Length];
    file.Read(bytes, 0, (int)file.Length);
    ms.Write(bytes, 0, (int)file.Length);
}

It’s worth noting that if the files are enormous, the reading operation will need twice as much memory as the overall file size. One alternative is to create the MemoryStream from the byte array; however, the code below assumes that you will not write to that stream.

MemoryStream ms = new MemoryStream(bytes, writable: false);

According to my investigation (below), the internal buffer is the same byte array as the one you provide it, saving memory.

byte[] testData = new byte[] { 104, 105, 121, 97 };
var ms = new MemoryStream(testData, 0, 4, false, true);
Assert.AreSame(testData, ms.GetBuffer());

Answered by Rob Church

Solution #4

If you’re seeking for the condensed versions, here’s what you’ll find:

var memoryStream = new MemoryStream(File.ReadAllBytes("1.dat"));

File.WriteAllBytes("1.dat", memoryStream.ToArray()); 

Answered by Slai

Solution #5

For writing to a file, the combined solution is:

MemoryStream ms = new MemoryStream();    
FileStream file = new FileStream("file.bin", FileMode.Create, FileAccess.Write);
ms.WriteTo(file);
file.Close();
ms.Close();

Answered by Berkay Turancı

Post is based on https://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file