Skip to content

[API Proposal]: Expose SafeFileHandle.CanSeek #58370

@adamsitnik

Description

@adamsitnik

Background and motivation

Recently, we have introduced new RandomAccess type (#24847) which exposes some convenient methods for File IO. To use any of these methods, users need to provide a SafeFileHandle instance which can be obtained by using File.OpenHandle (also a new method introduced in .NET 6).

As of today, all these methods support only seekable files. In contrary to FileStream, SafeFileHandle does not expose CanSeek property, so the users can't easily check whether they can use the new RandomAccess type methods.

API Proposal

namespace Microsoft.Win32.SafeHandles
{
    partial class SafeFileHandle
    {
        public bool CanSeek { get; }
    }
}

API Usage

using SafeFileHandle fileHandle = File.OpenHandle(path, FileMode.Open, FileAccess.Read);
long fileLength = fileHandle.CanSeek ? RandomAccess.GetLength(fileHandle) : -1;

Risks

None. The API has been already implemented for both Windows and Unix and it's being used by FileStream, but it's internal.

Metadata

Metadata

Assignees

Labels

api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.IO

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions