FileStream.Write Method

Writes a block of bytes to this stream using data from a buffer.

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

public override void Write(
	byte[] array,
	int offset,
	int count


Type: System.Byte[]

The buffer containing data to write to the stream.

Type: System.Int32

The zero-based byte offset in array at which to begin copying bytes to the current stream.

Type: System.Int32

The number of bytes to be written to the current stream.


array is null.


offset and count describe an invalid range in array.


offset or count is negative.


An I/O error occurs.

- or -

Another thread may have caused an unexpected change in the position of the operating system's file handle.


The stream is closed.


The current stream instance does not support writing.

This method overrides Write.

The offset parameter gives the offset of the byte in array (the buffer index) at which to begin copying, and the count parameter gives the number of bytes that will be written to the stream. If the write operation is successful, the current position of the stream is advanced by the number of bytes written. If an exception occurs, the current position of the stream is unchanged.


Use the CanWrite property to determine whether the current instance supports writing. For additional information, see CanWrite.

Do not interrupt a thread that is performing a write operation. Although the operation may appear to be successful after the thread is unblocked, the interruption can affect your application's performance and reliability.

For a list of common I/O tasks, see Common I/O Tasks.

This code example is part of a larger example provided for the Lock method.

// Write the original file data. 
if(fileStream.Length == 0)
    tempString = 
        lastRecordText + recordNumber.ToString();
        0, uniEncoding.GetByteCount(tempString));

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
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft