MemoryStream

MemoryStream to file

The MemoryStream creates a stream whose backing store is memory. Actually, it represents a pure, in-memory stream of data. Memory is much faster when compared to disk or network accesses.

The following section explains :

# MemoryStream to File

# MemoryStream to String

MemoryStream to FileStream

C# MemoryStream to a file

With MemoryStream, you can act upon the byte[] stored in memory rather than a file or other resource. Use a byte[] because it is a fixed sized object making it easier for memory allocation and cleanup and holds relatively no overhead, especially since you don't need to use the functions of the MemoryStream.

Save Stream to File

C# MemoryStream to a file

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string memString = "Memory test string !!";
            // convert string to stream
            byte[] buffer = Encoding.ASCII.GetBytes(memString);
            MemoryStream ms = new MemoryStream(buffer);
            //write to file
            FileStream file = new FileStream("d:\\file.txt", FileMode.Create, FileAccess.Write);
            ms.WriteTo(file);
            file.Close();
            ms.Close();
        }
    }
}
VB.Net MemoryStream to a file

Save and load MemoryStream to a file

VB.Net MemoryStream to a file

Imports System.IO
Imports System.Text
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim memString As String = "Memory test string !!!"
        ' convert string to stream
        Dim buffer As Byte() = Encoding.ASCII.GetBytes(memString)
        Dim ms As New MemoryStream(buffer)
        'write to file
        Dim file As New FileStream("d:\file.txt", FileMode.Create, FileAccess.Write)
        ms.WriteTo(file)
        file.Close()
        ms.Close()
    End Sub
End Class

Save MemoryStream to a String

The following program shows how to Read from memorystream to a string. Steps follows..

StreamWriter sw = new StreamWriter(memoryStream);
sw.WriteLine("Your string to Memoery");

This string is currently saved in the StreamWriters buffer. Flushing the stream will force the string whose backing store is memory (MemoryStream).

How do you get a string from a MemoryStream

Next step is to read this string from memorystream.

ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string  myStr = sr.ReadToEnd();
Serializing a memorystream object to string

The StreamReader will read from the current position of the MemoryStream which is currently set at the end of the string earlier we just wrote to it. We have to set the position to 0 in order to read from the start.

Save MemoryStream to a String - C#

using System;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                StreamWriter sw = new StreamWriter(ms);
                sw.WriteLine("Hello World !!");
                sw.Flush();
                ms.Position = 0;
                StreamReader sr = new StreamReader(ms);
                string  myStr = sr.ReadToEnd();
                MessageBox.Show(myStr);
            }
        }
    }
}

Save MemoryStream to a String - VB.Net

Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using ms As New MemoryStream()
            Dim sw As New StreamWriter(ms)
            sw.WriteLine("Hello World !!")
            sw.Flush()
            ms.Position = 0
            Dim sr As New StreamReader(ms)
            Dim myStr As String = sr.ReadToEnd()
            MessageBox.Show(myStr)
        End Using
    End Sub
End Class

There is another methos also help us to read from Memory stream.

Encoding.ASCII.GetString(ms.ToArray());

C# Source Code

using System;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                StreamWriter sw = new StreamWriter(ms);
                sw.WriteLine("Hello World !!!");
                sw.Flush();
                string myStr = Encoding.ASCII.GetString(ms.ToArray ());
                MessageBox.Show(myStr);
            }
        }
    }
}

VB.Net Source Code

Imports System.IO
Imports System.Text
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using ms As New MemoryStream()
            Dim sw As New StreamWriter(ms)
            sw.WriteLine("Hello World !!!")
            sw.Flush()
            Dim myStr As String = Encoding.ASCII.GetString(ms.ToArray())
            MessageBox.Show(myStr)
        End Using
    End Sub
End Class

NEXT.....Parse XML - XmlReader