Language

RandomAccess.ReadAsync Method

Definition

Overloads

Name Description
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.

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

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

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

public static System.Threading.Tasks.ValueTask<long> ReadAsync(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Collections.Generic.IReadOnlyList<Memory<byte>> buffers, long fileOffset, System.Threading.CancellationToken cancellationToken = default);
static member ReadAsync : Microsoft.Win32.SafeHandles.SafeFileHandle * System.Collections.Generic.IReadOnlyList<Memory<byte>> * int64 * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int64>
Public Shared Function ReadAsync (handle As SafeFileHandle, buffers As IReadOnlyList(Of Memory(Of Byte)), fileOffset As Long, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Long)

Parameters

handle
SafeFileHandle

The file handle.

buffers
IReadOnlyList<Memory<Byte>>

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

fileOffset
Int64

The file position to read from. In .NET 11 and later versions, this parameter is ignored for files that don't support seeking, such as pipes, sockets, and character devices. The value must be non-negative even when it's ignored.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

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.

In .NET 10 and earlier versions, the file doesn't support seeking (for example, a pipe or socket).

fileOffset is negative.

handle was not opened for reading.

An I/O error occurred.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For files that support seeking, the file position is not advanced.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Read(SafeFileHandle, IReadOnlyList<Memory<Byte>>, Int64).

Applies to

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

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

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

public static System.Threading.Tasks.ValueTask<int> ReadAsync(Microsoft.Win32.SafeHandles.SafeFileHandle handle, Memory<byte> buffer, long fileOffset, System.Threading.CancellationToken cancellationToken = default);
static member ReadAsync : Microsoft.Win32.SafeHandles.SafeFileHandle * Memory<byte> * int64 * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Shared Function ReadAsync (handle As SafeFileHandle, buffer As Memory(Of Byte), fileOffset As Long, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

Parameters

handle
SafeFileHandle

The file handle.

buffer
Memory<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. In .NET 11 and later versions, this parameter is ignored for files that don't support seeking, such as pipes, sockets, and character devices. The value must be non-negative even when it's ignored.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

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.

In .NET 10 and earlier versions, the file doesn't support seeking (for example, a pipe or socket).

fileOffset is negative.

handle was not opened for reading.

An I/O error occurred.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For files that support seeking, the file position is not advanced.

Applies to