Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

MemoryStream.Read Method

Reads a block of bytes from the current stream and writes the data to buffer.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public override int Read(
	byte[] buffer,
	int offset,
	int count
)

Parameters

buffer
Type: System.Byte[]

When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream.

offset
Type: System.Int32

The byte offset in buffer at which to begin reading.

count
Type: System.Int32

The maximum number of bytes to read.

Return Value

Type: System.Int32
The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.

ExceptionCondition
ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

offset subtracted from the buffer length is less than count.

ObjectDisposedException

The current stream instance is closed.

For an example of creating a file and writing text to a file, see How to: Write Text to a File. For an example of reading text from a file, see How to: Read Text from a File. For an example of reading from and writing to a binary file, see How to: Read and Write to a Newly Created Data File.

This method overrides Read.

The offset parameter gives the offset of the first byte in buffer to which data from the current stream is written. The count parameter gives the maximum number of bytes to read from the current stream. The returned value is the actual number of bytes read, or zero if the end of the stream is reached.

If the read operation is successful, the current position within the stream advances by the number of bytes read. If an exception occurs, the current position within the stream remains unchanged.

The Read method will return zero only if the end of the stream is reached. In all other cases, Read always reads at least one byte from the stream before returning. By definition, if no data is available from the stream upon a call to Read, the Read method returns zero (the end of the stream is reached automatically). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

Use BinaryReader for reading primitive data types.

Caution noteCaution:

If the byte array specified in the buffer parameter is the underlying buffer returned by the GetBuffer method, the array contents are overwritten, and no exception is thrown.

This code example is part of a larger example provided for the MemoryStream class.

// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft