Skip to content

CreateSafeFileHandle not documented #1962

@JeremyKuhne

Description

@JeremyKuhne

System.IO.WindowsRuntimeStorageExtensions (from the System.Runtime.WindowsRuntime library) had APIs added for converting from Windows.Storage.IStorageFile and Windows.Storage.IStorageFolder to Microsoft.Win32.SafeHandles.SafeFileHandle. These don't appear to be documented anywhere.

The key usage scenario is taking a WinRT IStorageFile and constructing a System.IO.FileStream around it. This allows synchronous access to IStorageFile/IStorageFolder objects.

The reference definitions:
/runtime/src/libraries/System.Runtime.WindowsRuntime/ref/System.Runtime.WindowsRuntime.cs#L41-L46 [edited to point to dotnet/runtime]:

        public static SafeFileHandle CreateSafeFileHandle(
            this IStorageFile windowsRuntimeFile,
            FileAccess access = FileAccess.ReadWrite,
            FileShare share = FileShare.Read,
            FileOptions options = FileOptions.None)

        public static SafeFileHandle CreateSafeFileHandle(
            this IStorageFolder rootDirectory,
            string relativePath,
            FileMode mode)

        public static SafeFileHandle CreateSafeFileHandle(
            this IStorageFolder rootDirectory,
            string relativePath,
            FileMode mode,
            FileAccess access,
            FileShare share = FileShare.Read,
            FileOptions options = FileOptions.None)

Arguments work the same as FileStream constructor arguments, with the obvious exception that you can provide a root directory and a relative path.

The API is present in 2.0 releases of System.IO.WindowsRuntime` for .NET 4.6.1, .NET Core 2.0, and Universal Windows Platform 10.0.16299.

Metadata

Metadata

Labels

Pri1Indicates issues/PRs that are high priorityexternalNot related to contentmigration-fixesIndicates PRs that are related to problems caused by the migration from MSDNnew-contentIndicates PRs that contain new articles

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions