SIGN UP MEMBER LOGIN:    
ARTICLE

How do I use Memory Stream in C#

Posted by Ashish Singhal Articles | How do I October 27, 2005
This article shows how to use memory stream to read byte[] array and can use to write some file in memory.
Reader Level:

This code shows how to use MemoryStream class and its member to read data in memory stream, which can be used to save it from there.

using System;
using
System.IO;
using
System.Text;
class
MemoryStreamSample
{
static void
Main()
{
int
count;
//GetByteData function to get Byte data like if you fetch Image column data from sqlserver or somewhere.
byte
[] byteArray = getByteData();
MemoryStream memStream =
new
MemoryStream(ByteArray);
// Write the second string to the stream, byte by byte.
count = 0;
// Write the stream properties to the console.
Console.WriteLine(
"Capacity = {0}, Length = {1}, Position = {2}\n",
memStream.Capacity.ToString(),
memStream.Length.ToString(),
memStream.Position.ToString());
// Set the position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin);
// Read the first 20 bytes from the stream.
byteArray = new byte
[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
// Read the remaining bytes, byte by byte.
while
(count < memStream.Length)
{
byteArray[count++] = Convert.ToByte(memStream.ReadByte());
}
// Decode the byte array into a char array
// and write it to the console.
charArray = new char
[uniEncoding.GetCharCount(byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

Way to copy from http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx

Posted by David Jul 11, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor