RandomAccess.Read Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Overloads

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.

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

Source:
RandomAccess.cs
Source:
RandomAccess.cs
Source:
RandomAccess.cs

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

C#
public static long Read(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Collections.Generic.IReadOnlyList<Memory<byte>> buffers, long fileOffset);

Parameters

handle
SafeFileHandle

The file handle.

buffers
IReadOnlyList<Memory<Byte>>

A list of memory buffers. When this method returns, the contents of the buffers are replaced by the bytes read from the file.

fileOffset
Int64

The file position to read from.

Returns

The total number of bytes read into the buffers. This can be less than the number of bytes allocated in the buffers if that many bytes are not currently available, or zero (0) if the end of the file has been reached.

Exceptions

handle or buffers is null.

handle is invalid.

The file is closed.

The file does not support seeking (pipe or socket).

fileOffset is negative.

handle was not opened for reading.

An I/O error occurred.

Remarks

Position of the file is not advanced.

Applies to

.NET 10 and other versions
Product Versions
.NET 6, 7, 8, 9, 10

Read(SafeFileHandle, Span<Byte>, Int64)

Source:
RandomAccess.cs
Source:
RandomAccess.cs
Source:
RandomAccess.cs

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

C#
public static int Read(Microsoft.Win32.SafeHandles.SafeFileHandle handle, Span<byte> buffer, long fileOffset);

Parameters

handle
SafeFileHandle

The file handle.

buffer
Span<Byte>

A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the file.

fileOffset
Int64

The file position to read from.

Returns

The total number of bytes read into the buffer. This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the file has been reached.

Exceptions

handle is null.

handle is invalid.

The file is closed.

The file does not support seeking (pipe or socket).

fileOffset is negative.

handle was not opened for reading.

An I/O error occurred.

Remarks

Position of the file is not advanced.

Applies to

.NET 10 and other versions
Product Versions
.NET 6, 7, 8, 9, 10