FileStream.Write Method

Definition

Overloads

Write(ReadOnlySpan<Byte>)

Writes a sequence of bytes from a read-only span to the current file stream and advances the current position within this file stream by the number of bytes written.

Write(Byte[], Int32, Int32)

Writes a block of bytes to the file stream.

Write(ReadOnlySpan<Byte>)

Source:
FileStream.cs
Source:
FileStream.cs
Source:
FileStream.cs

Writes a sequence of bytes from a read-only span to the current file stream and advances the current position within this file stream by the number of bytes written.

public override void Write (ReadOnlySpan<byte> buffer);

Parameters

buffer
ReadOnlySpan<Byte>

A region of memory. This method copies the contents of this region to the current file stream.

Exceptions

.NET 8 and later versions: The underlying pipe is closed or disconnected.

Remarks

Use the CanWrite property to determine whether the current instance supports writing. Use the WriteAsync method to write asynchronously to the current stream.

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Write(Byte[], Int32, Int32)

Source:
FileStream.cs
Source:
FileStream.cs
Source:
FileStream.cs

Writes a block of bytes to the file stream.

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

Parameters

arraybuffer
Byte[]

The buffer containing data to write to the stream.

offset
Int32

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

count
Int32

The maximum number of bytes to write.

Exceptions

array is null.

offset and count describe an invalid range in array.

offset or count is negative.

An I/O error occurred.

-or-

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

-or-

.NET 8 and later versions: The underlying pipe is closed or disconnected.

The stream is closed.

The current stream instance does not support writing.

Examples

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();
    fileStream.Write(uniEncoding.GetBytes(tempString),
        0, uniEncoding.GetByteCount(tempString));
}

Remarks

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.

Note

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

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

For a list of common file and directory operations, see Common I/O Tasks.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0