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.

FileStream.Write Method

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

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


The array to which bytes are written.
The byte offset in array at which to begin writing.
The maximum number of bytes to write.


Exception Type Condition
ArgumentNullException array is a null reference (Nothing in Visual Basic).
ArgumentException offset and count describe an invalid range in array.
ArgumentOutOfRangeException offset or count is negative.
IOException An I/O error occurs.
ObjectDisposedException The stream is closed.
NotSupportedException 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 writing, and the count parameter gives the maximum number of bytes that will be written to this 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.

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

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File



Rename or move a file. File.Move


Copy a file. File.Copy


Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
Create a directory. Directory.CreateDirectory



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

[Visual Basic] 
' Write the original file data.
If aFileStream.Length = 0 Then
    tempString = _
        lastRecordText + recordNumber.ToString()
    aFileStream.Write(uniEncoding.GetBytes(tempString), _
        0, uniEncoding.GetByteCount(tempString))
End If

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

// Write the original file data.
if(fileStream->Length == 0)
    tempString = String::Concat(
        lastRecordText, recordNumber.ToString());
        0, uniEncoding->GetByteCount(tempString));

[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

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

© 2017 Microsoft