C# MemoryStream Example

C# MemoryStream Example

C# MemoryStream Example C# memoryStream is derived class inherited from Stream class. When you use Memory Stream it loads entire file into memory of the system and there is no disk I/O operations. As you are dealing with direct memory the performance of application is very high. It is useful when you do have plenty of physical memory [RAM] available for application. Here we will see both C#.net and Vb.net example of how to use memory stream class. If you want to process the files faster, use MemoryStream instance. Example here is very easy to understand and scope is very simple.

C# Memory stream example.

The example uses static method of File ReadAllBytes. It opens binary file and returns byte array. Stream reader is used to read content of actual file. Let's see C# example.

using System;
using System.IO;
namespace Csharp.MemoryStream.Example
  class Program
    static void Main(string[] args)
    byte []arrBytes = File.ReadAllBytes(@"C:\Memory_Stream_Example.txt");
    MemoryStream ms = new MemoryStream(arrBytes);
    StreamReader reader = new StreamReader(ms);
VB.net MemoryStream Example VB.net Memory stream example.
Imports System
Imports System.IO
Namespace Csharp.MemoryStream.Example
 Class Program
	Private Shared Sub Main(args As String())

        	Dim arrBytes As Byte() = File.ReadAllBytes("C:\Memory_Stream_Example.txt")

		Dim ms As New MemoryStream(arrBytes)

		Dim reader As New StreamReader(ms)




		End Sub
        End Class
End Namespace
OutPut: Here are content of text file.

This is Example of Memory Stream.
The file is loaded into physical memory.
There is no disk input output operations, hence performance is

This is very basic example of using memory stream instance in Dot net language.


C# XML XmlReaderC# Recursion ExampleC# Do While Loop ExampleDateTimeTryParse using C# and VB.net exampleC# foreach loopC# HtmlTextWriter ExampleOne dimensional array example C#.net VB.netC# Get Random Item From List (VB.net) Example.C# 4.0 Tuple ExampleC# while loopC# For Loop


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.