Ejemplo n.º 1
0
        /// <summary>
        /// CreateFile2 wrapper. Only available on Windows 8 and above.
        /// </summary>
        public static SafeFileHandle CreateFile2(
            string path,
            DesiredAccess desiredAccess,
            ShareMode shareMode,
            CreationDisposition creationDisposition,
            FileAttributes fileAttributes     = FileAttributes.NONE,
            FileFlags fileFlags               = FileFlags.NONE,
            SecurityQosFlags securityQosFlags = SecurityQosFlags.NONE)
        {
            CREATEFILE2_EXTENDED_PARAMETERS extended = new CREATEFILE2_EXTENDED_PARAMETERS();

            extended.dwSize             = (uint)Marshal.SizeOf <CREATEFILE2_EXTENDED_PARAMETERS>();
            extended.dwFileAttributes   = fileAttributes;
            extended.dwFileFlags        = fileFlags;
            extended.dwSecurityQosFlags = securityQosFlags;
            unsafe
            {
                extended.lpSecurityAttributes = null;
            }
            extended.hTemplateFile = IntPtr.Zero;

            SafeFileHandle handle = Direct.CreateFile2(
                lpFileName: path,
                dwDesiredAccess: desiredAccess,
                dwShareMode: shareMode,
                dwCreationDisposition: creationDisposition,
                pCreateExParams: ref extended);

            if (handle.IsInvalid)
            {
                throw ErrorHelper.GetIoExceptionForLastError();
            }

            return(handle);
        }