Esempio n. 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));
        }
        private static HANDLE_SHARING_OPTIONS FileShareToHandleSharingOptions(FileShare share)
        {
            if ((share & FileShare.Inheritable) != 0)
            {
                throw new NotSupportedException(SR.NotSupported_Inheritable);
            }
            if (share < FileShare.None || share > (FileShare.ReadWrite | FileShare.Delete))
            {
                throw new ArgumentOutOfRangeException(nameof(share), share, null);
            }

            HANDLE_SHARING_OPTIONS sharingOptions = HANDLE_SHARING_OPTIONS.HSO_SHARE_NONE;

            if ((share & FileShare.Read) != 0)
            {
                sharingOptions |= HANDLE_SHARING_OPTIONS.HSO_SHARE_READ;
            }
            if ((share & FileShare.Write) != 0)
            {
                sharingOptions |= HANDLE_SHARING_OPTIONS.HSO_SHARE_WRITE;
            }
            if ((share & FileShare.Delete) != 0)
            {
                sharingOptions |= HANDLE_SHARING_OPTIONS.HSO_SHARE_DELETE;
            }

            return(sharingOptions);
        }
Esempio n. 3
0
        public static SafeFileHandle CreateSafeFileHandle(
            this IStorageFolder rootDirectory,
            string relativePath,
            FileMode mode,
            FileAccess access,
            FileShare share     = FileShare.Read,
            FileOptions options = FileOptions.None)
        {
            if (rootDirectory == null)
            {
                throw new ArgumentNullException(nameof(rootDirectory));
            }
            if (relativePath == null)
            {
                throw new ArgumentNullException(nameof(relativePath));
            }

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

            IStorageFolderHandleAccess handleAccess = ((object)rootDirectory) as IStorageFolderHandleAccess;

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

            SafeFileHandle handle;

            int result = handleAccess.Create(
                relativePath,
                creationOptions,
                accessOptions,
                sharingOptions,
                handleOptions,
                IntPtr.Zero,
                out handle);

            if (result != 0)
            {
                throw Win32Marshal.GetExceptionForWin32Error(Win32Marshal.TryMakeWin32ErrorCodeFromHR(result), relativePath);
            }

            return(handle);
        }
Esempio n. 4
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 = ((object)windowsRuntimeFile) as IStorageItemHandleAccess;

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

            SafeFileHandle handle;

            int result = handleAccess.Create(
                accessOptions,
                sharingOptions,
                handleOptions,
                IntPtr.Zero,
                out handle);

            if (result != 0)
            {
                throw Win32Marshal.GetExceptionForWin32Error(Win32Marshal.TryMakeWin32ErrorCodeFromHR(result), windowsRuntimeFile.Name);
            }

            return(handle);
        }
Esempio n. 5
0
        public static SafeFileHandle CreateSafeFileHandle(
            this IStorageFolder rootDirectory,
            string relativePath,
            FileMode mode,
            FileAccess access,
            FileShare share     = FileShare.Read,
            FileOptions options = FileOptions.None)
        {
            if (rootDirectory == null)
            {
                throw new ArgumentNullException(nameof(rootDirectory));
            }
            if (relativePath == null)
            {
                throw new ArgumentNullException(nameof(relativePath));
            }

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

            IStorageFolderHandleAccess handleAccess = rootDirectory.As <IStorageFolderHandleAccess>();

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

            return(handleAccess.Create(
                       relativePath,
                       creationOptions,
                       accessOptions,
                       sharingOptions,
                       handleOptions,
                       IntPtr.Zero));
        }
 public int Create([NativeTypeName("LPCWSTR")] ushort *fileName, HANDLE_CREATION_OPTIONS creationOptions, HANDLE_ACCESS_OPTIONS accessOptions, HANDLE_SHARING_OPTIONS sharingOptions, HANDLE_OPTIONS options, IOplockBreakingHandler *oplockBreakingHandler, [NativeTypeName("HANDLE *")] IntPtr *interopHandle)
 {
     return(((delegate * unmanaged <IStorageFolderHandleAccess *, ushort *, HANDLE_CREATION_OPTIONS, HANDLE_ACCESS_OPTIONS, HANDLE_SHARING_OPTIONS, HANDLE_OPTIONS, IOplockBreakingHandler *, IntPtr *, int>)(lpVtbl[3]))((IStorageFolderHandleAccess *)Unsafe.AsPointer(ref this), fileName, creationOptions, accessOptions, sharingOptions, options, oplockBreakingHandler, interopHandle));
 }
 public int Create(HANDLE_ACCESS_OPTIONS accessOptions, HANDLE_SHARING_OPTIONS sharingOptions, HANDLE_OPTIONS options, IOplockBreakingHandler *oplockBreakingHandler, [NativeTypeName("HANDLE *")] IntPtr *interopHandle)
 {
     return(((delegate * unmanaged <IStorageItemHandleAccess *, HANDLE_ACCESS_OPTIONS, HANDLE_SHARING_OPTIONS, HANDLE_OPTIONS, IOplockBreakingHandler *, IntPtr *, int>)(lpVtbl[3]))((IStorageItemHandleAccess *)Unsafe.AsPointer(ref this), accessOptions, sharingOptions, options, oplockBreakingHandler, interopHandle));
 }