Пример #1
0
        public static SafeFileHandle CreateSafeFileHandle(
            this IStorageFile windowsRuntimeFile,
            FileAccess access   = FileAccess.ReadWrite,
            FileShare share     = FileShare.Read,
            FileOptions options = FileOptions.None)
        {
            if (windowsRuntimeFile == null)
            {
                throw new ArgumentNullException(nameof(windowsRuntimeFile));
            }

            HANDLE_ACCESS_OPTIONS  accessOptions  = FileAccessToHandleAccessOptions(access);
            HANDLE_SHARING_OPTIONS sharingOptions = FileShareToHandleSharingOptions(share);
            HANDLE_OPTIONS         handleOptions  = FileOptionsToHandleOptions(options);

            IStorageItemHandleAccess handleAccess = windowsRuntimeFile.As <IStorageItemHandleAccess>();

            if (handleAccess == null)
            {
                return(null);
            }

            return(handleAccess.Create(
                       accessOptions,
                       sharingOptions,
                       handleOptions,
                       IntPtr.Zero));
        }