This documentation is archived and is not being maintained.

MemoryStream.ToArray Method

Writes the entire stream contents to a byte array, regardless of the Position property.

[Visual Basic]
Public Overridable Function ToArray() As Byte()
[C#]
public virtual byte[] ToArray();
[C++]
public: virtual unsigned char ToArray()  __gc[];
[JScript]
public function ToArray() : Byte[];

Return Value

A new byte array.

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

This method returns a copy of the contents of the MemoryStream as a byte array. If the current instance was constructed on a provided byte array, a copy of the section of the array to which this instance has access is returned. See the MemoryStream(Byte[], Int32, Int32) constructor for details.

Note   This method works when the MemoryStream is closed.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

MemoryStream Class | MemoryStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show: