Language

RandomAccess Class

Definition

Provides offset-based APIs for reading and writing files in a thread-safe manner.

public ref class RandomAccess abstract sealed
public static class RandomAccess
type RandomAccess = class
Public Class RandomAccess
Inheritance
RandomAccess

Remarks

In .NET 10 and earlier versions, the read and write methods support only files that support seeking.

Starting with .NET 11, the read and write methods also support non-seekable handles, such as pipes, sockets, and character devices. For these handles, the fileOffset parameter is ignored, but its value must still be non-negative.

For files that support seeking, read and write operations use the specified offset without advancing the file position. GetLength and SetLength require a handle that supports seeking in all versions.

Methods

Name Description
FlushToDisk(SafeFileHandle)

Flushes the operating system buffers for the given file to disk.

GetLength(SafeFileHandle)

Gets the length of the file in bytes.

Read(SafeFileHandle, IReadOnlyList<Memory<Byte>>, Int64)

Reads a sequence of bytes from given file at given offset.

Read(SafeFileHandle, Span<Byte>, Int64)

Reads a sequence of bytes from given file at given offset.

ReadAsync(SafeFileHandle, IReadOnlyList<Memory<Byte>>, Int64, CancellationToken)

Reads a sequence of bytes from given file at given offset.

ReadAsync(SafeFileHandle, Memory<Byte>, Int64, CancellationToken)

Reads a sequence of bytes from given file at given offset.

SetLength(SafeFileHandle, Int64)

Sets the length of the file to the given value.

Write(SafeFileHandle, IReadOnlyList<ReadOnlyMemory<Byte>>, Int64)

Writes a sequence of bytes from given buffers to given file at given offset.

Write(SafeFileHandle, ReadOnlySpan<Byte>, Int64)

Writes a sequence of bytes from given buffer to given file at given offset.

WriteAsync(SafeFileHandle, IReadOnlyList<ReadOnlyMemory<Byte>>, Int64, CancellationToken)

Writes a sequence of bytes from given buffers to given file at given offset.

WriteAsync(SafeFileHandle, ReadOnlyMemory<Byte>, Int64, CancellationToken)

Writes a sequence of bytes from given buffer to given file at given offset.

Applies to

See also