Skip to content

File.OpenHandle() limitations #79335

@iSazonov

Description

@iSazonov

We started to replace custom CreateFileW pinvokes with File.OpenHandle() in PowerShell repository and was blocked because of the API limitations.
Currently there are strong argument validations:

if (mode < FileMode.CreateNew || mode > FileMode.Append)
{
badArg = nameof(mode);
}
else if (access < FileAccess.Read || access > FileAccess.ReadWrite)
{
badArg = nameof(access);
}
else if (tempshare < FileShare.None || tempshare > (FileShare.ReadWrite | FileShare.Delete))
{
badArg = nameof(share);
}

CreateFileW allows to use many flags for these parameters. Now they are blocked. In particular, we would like to have FILE_FLAG_POSIX_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT, FILE_FLAG_BACKUP_SEMANTICS, FileAccess = 0 and so on.

What is the preferred way to solve this problem without using custom pinvoke?

Is it possible to remove these hard checks from the API so that we can use any flags with a simple cast?

The same question for another APIs like File.SetAttributes().

/cc @adamsitnik

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions