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.

Stream.Write Method

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

[Visual Basic]
Public MustOverride Sub Write( _
   ByVal buffer() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
)
[C#]
public abstract void Write(
 byte[] buffer,
 int offset,
 int count
);
[C++]
public: virtual void Write(
 unsigned char buffer __gc[],
 int offset,
 int count
) = 0;
[JScript]
public abstract function Write(
   buffer : Byte[],
 offset : int,
 count : int
);

Parameters

buffer
An array of bytes. This method copies count bytes from buffer to the current stream.
offset
The zero-based byte offset in buffer at which to begin copying bytes to the current stream.
count
The number of bytes to be written to the current stream.

Exceptions

Exception Type Condition
ArgumentException The sum of offset and count is greater than the buffer length.
ArgumentNullException buffer is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException offset or count is negative.
IOException An I/O error occurs.
NotSupportedException The stream does not support writing.
ObjectDisposedException Methods were called after the stream was closed.

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.

Use the CanWrite property to determine whether the current instance supports writing.

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

The default implementation calls the asynchronous BeginWrite method.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the Write method to copy an input stream to an output stream.

[Visual Basic] 
Const size As Integer = 4096
Dim bytes(4096) As Byte
Dim numBytes As Integer
numBytes = input.Read(bytes, 0, size)
While numBytes > 0
    output.Write(bytes, 0, numBytes)
    numBytes = input.Read(bytes, 0, size)
End While

[C#] 
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while((numBytes = input.Read(bytes, 0, size)) > 0)
    output.Write(bytes, 0, numBytes);

[C++] 
const int size = 4096;
Byte bytes[] = new Byte[4096];
int numBytes;
while((numBytes = input->Read(bytes, 0, size)) > 0)
    output->Write(bytes, 0, numBytes);

[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.

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

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft