Windows apps
Collapse the table of content
Expand the table of content
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.Write Method

Writes a block of bytes to the current stream using data read from buffer.

[Visual Basic]
Overrides Public Sub Write( _
   ByVal buffer() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
public override void Write(
 byte[] buffer,
 int offset,
 int count
public: void Write(
 unsigned char buffer __gc[],
 int offset,
 int count
public override function Write(
   buffer : Byte[],
 offset : int,
 count : int


The buffer to write data from.
The byte offset in buffer at which to begin writing from.
The maximum number of bytes to write.


Exception Type Condition
ArgumentNullException buffer is a null reference (Nothing in Visual Basic).
NotSupportedException The stream does not support writing. For additional information see CanWrite.


The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified.

ArgumentException offset subtracted from the buffer length is less than count.
ArgumentOutOfRangeException offset or count are negative.
IOException An I/O error occurs.
ObjectDisposedException The current stream instance is closed.


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 overrides Write.

The offset parameter gives the offset of the first byte in buffer to write from, and the count parameter gives the number of bytes to write. If the write operation is successful, the current position within the stream is advanced by the number of bytes written. If an exception occurs, the current position within the stream is unchanged.

Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.


[Visual Basic, C#, C++] This code example is part of a larger example provided for the MemoryStream class.

[Visual Basic] 
' Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length)

// Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length);

// Write the first string to the stream.
memStream->Write(firstString, 0 , firstString->Length);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


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

© 2017 Microsoft