Export (0) Print
Expand All

MemoryStream.Write Method

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

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

'Declaration
Public Overrides Sub Write ( _
	buffer As Byte(), _
	offset As Integer, _
	count As Integer _
)
'Usage
Dim instance As MemoryStream
Dim buffer As Byte()
Dim offset As Integer
Dim count As Integer

instance.Write(buffer, offset, count)
public void Write (
	byte[] buffer, 
	int offset, 
	int count
)
public override function Write (
	buffer : byte[], 
	offset : int, 
	count : int
)
Not applicable.

Parameters

buffer

The buffer to write data from.

offset

The byte offset in buffer at which to begin writing from.

count

The maximum number of bytes to write.

Exception typeCondition

ArgumentNullException

buffer is a null reference (Nothing in Visual Basic).

NotSupportedException

The stream does not support writing. For additional information see CanWrite.

-or-

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.

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

' 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);

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft