/// <summary> /// CreateFile wrapper that attempts to use CreateFile2 if running as Windows Store app. /// </summary> public static SafeFileHandle CreateFile( StringSpan path, CreationDisposition creationDisposition, DesiredAccess desiredAccess = DesiredAccess.GenericReadWrite, ShareModes shareMode = ShareModes.ReadWrite, FileAttributes fileAttributes = FileAttributes.None, FileFlags fileFlags = FileFlags.None, SecurityQosFlags securityQosFlags = SecurityQosFlags.None) { // Prefer CreateFile2, falling back to CreateFileEx if we can if (s_createFileDelegate == null) { s_createFileDelegate = CreateFile2; try { return(s_createFileDelegate(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags)); } catch (EntryPointNotFoundException) { s_createFileDelegate = Delegates.CreateDelegate <CreateFileDelegate>( "WInterop.Storage.Desktop.NativeMethods, " + Delegates.DesktopLibrary, "CreateFileW"); } } return(s_createFileDelegate(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags)); }
/// <summary> /// CreateFile2 wrapper. Only available on Windows 8 and above. /// </summary> public static unsafe SafeFileHandle CreateFile2( StringSpan path, DesiredAccess desiredAccess, ShareModes shareMode, CreationDisposition creationDisposition, FileAttributes fileAttributes = FileAttributes.None, FileFlags fileFlags = FileFlags.None, SecurityQosFlags securityQosFlags = SecurityQosFlags.None) { CREATEFILE2_EXTENDED_PARAMETERS extended = new CREATEFILE2_EXTENDED_PARAMETERS() { dwSize = (uint)sizeof(CREATEFILE2_EXTENDED_PARAMETERS), dwFileAttributes = fileAttributes, dwFileFlags = fileFlags, dwSecurityQosFlags = securityQosFlags }; SafeFileHandle handle = Imports.CreateFile2( lpFileName: ref MemoryMarshal.GetReference(path.GetSpanWithoutTerminator()), dwDesiredAccess: desiredAccess, dwShareMode: shareMode, dwCreationDisposition: creationDisposition, pCreateExParams: ref extended); if (handle.IsInvalid) { throw Error.GetExceptionForLastError(path.ToString()); } return(handle); }
/// <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); }
/// <summary> /// CreateFile wrapper that attempts to use CreateFile2 if running as Windows Store app. /// </summary> public static SafeFileHandle CreateFile( string path, DesiredAccess desiredAccess, ShareMode shareMode, CreationDisposition creationDisposition, FileAttributes fileAttributes = FileAttributes.NONE, FileFlags fileFlags = FileFlags.NONE, SecurityQosFlags securityQosFlags = SecurityQosFlags.NONE) { // Prefer CreateFile2, falling back to CreateFileEx if we can if (s_createFileDelegate == null) { s_createFileDelegate = CreateFile2; try { return(s_createFileDelegate(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags)); } catch (Exception exception) { // Any error other than EntryPointNotFound we've found CreateFile2, rethrow if (!ErrorHelper.IsEntryPointNotFoundException(exception)) { throw; } s_createFileDelegate = Delegates.CreateDelegate <CreateFileDelegate>( "WInterop.FileManagement.Desktop.NativeMethods, " + Delegates.DesktopLibrary, "CreateFileW"); } } return(s_createFileDelegate(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags)); }
/// <summary> /// CreateFile2 wrapper. Only available on Windows 8 and above. /// </summary> public static unsafe SafeFileHandle CreateFile2( string path, DesiredAccess desiredAccess, ShareModes shareMode, CreationDisposition creationDisposition, FileAttributes fileAttributes = FileAttributes.None, FileFlags fileFlags = FileFlags.None, SecurityQosFlags securityQosFlags = SecurityQosFlags.None) { CREATEFILE2_EXTENDED_PARAMETERS extended = new CREATEFILE2_EXTENDED_PARAMETERS() { dwSize = (uint)sizeof(CREATEFILE2_EXTENDED_PARAMETERS), dwFileAttributes = fileAttributes, dwFileFlags = fileFlags, dwSecurityQosFlags = securityQosFlags }; SafeFileHandle handle = Imports.CreateFile2( lpFileName: path, dwDesiredAccess: desiredAccess, dwShareMode: shareMode, dwCreationDisposition: creationDisposition, pCreateExParams: ref extended); if (handle.IsInvalid) { throw Errors.GetIoExceptionForLastError(path); } return(handle); }
/// <summary> /// Wrapper that allows using System.IO defines where available. Calls CreateFile2 if available. /// </summary> public static SafeFileHandle CreateFileSystemIo( StringSpan path, System.IO.FileAccess fileAccess, System.IO.FileShare fileShare, System.IO.FileMode fileMode, System.IO.FileAttributes fileAttributes = 0, FileFlags fileFlags = FileFlags.None, SecurityQosFlags securityFlags = SecurityQosFlags.None) { return(CreateFile( path: path, desiredAccess: Conversion.FileAccessToDesiredAccess(fileAccess), shareMode: Conversion.FileShareToShareMode(fileShare), creationDisposition: Conversion.FileModeToCreationDisposition(fileMode), fileAttributes: (FileAttributes)fileAttributes, fileFlags: fileFlags, securityQosFlags: securityFlags)); }
/// <summary> /// Get a stream for the specified file. /// </summary> public static System.IO.Stream CreateFileStream( StringSpan path, DesiredAccess desiredAccess, ShareModes shareMode, CreationDisposition creationDisposition, FileAttributes fileAttributes = FileAttributes.None, FileFlags fileFlags = FileFlags.None, SecurityQosFlags securityQosFlags = SecurityQosFlags.None) { var fileHandle = CreateFile(path, creationDisposition, desiredAccess, shareMode, fileAttributes, fileFlags, securityQosFlags); // FileStream will own the lifetime of the handle return(new System.IO.FileStream( handle: fileHandle, access: Conversion.DesiredAccessToFileAccess(desiredAccess), bufferSize: 4096, isAsync: (fileFlags & FileFlags.Overlapped) != 0)); }
/// <summary> /// Get a stream for the specified file. /// </summary> public static System.IO.Stream CreateFileStream( string path, DesiredAccess desiredAccess, ShareMode shareMode, CreationDisposition creationDisposition, FileAttributes fileAttributes = FileAttributes.NONE, FileFlags fileFlags = FileFlags.NONE, SecurityQosFlags securityQosFlags = SecurityQosFlags.NONE) { var fileHandle = CreateFile(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags); // FileStream will own the lifetime of the handle return(new System.IO.FileStream( handle: fileHandle, access: Conversion.DesiredAccessToFileAccess(desiredAccess), bufferSize: 4096, isAsync: (fileFlags & FileFlags.FILE_FLAG_OVERLAPPED) != 0)); }
/// <summary> /// Wrapper that allows getting a file stream using System.IO defines. /// </summary> public static System.IO.Stream CreateFileStream( string path, System.IO.FileAccess fileAccess, System.IO.FileShare fileShare, System.IO.FileMode fileMode, System.IO.FileAttributes fileAttributes = 0, FileFlags fileFlags = FileFlags.NONE, SecurityQosFlags securityFlags = SecurityQosFlags.NONE) { return(CreateFileStream( path: path, desiredAccess: Conversion.FileAccessToDesiredAccess(fileAccess), shareMode: Conversion.FileShareToShareMode(fileShare), creationDisposition: Conversion.FileModeToCreationDisposition(fileMode), fileAttributes: (FileAttributes)fileAttributes, fileFlags: fileFlags, securityQosFlags: securityFlags)); }
/// <summary> /// CreateFile wrapper. Desktop only. Prefer File.CreateFile() as it will handle all supported platforms. /// </summary> /// <remarks>Not available in Windows Store applications.</remarks> public unsafe static SafeFileHandle CreateFileW( string path, DesiredAccess desiredAccess, ShareModes shareMode, CreationDisposition creationDisposition, FileAttributes fileAttributes = FileAttributes.None, FileFlags fileFlags = FileFlags.None, SecurityQosFlags securityQosFlags = SecurityQosFlags.None) { uint flags = (uint)fileAttributes | (uint)fileFlags | (uint)securityQosFlags; SafeFileHandle handle = Imports.CreateFileW(path, desiredAccess, shareMode, lpSecurityAttributes: null, creationDisposition, flags, hTemplateFile: IntPtr.Zero); if (handle.IsInvalid) { throw Error.GetExceptionForLastError(path); } return(handle); }
/// <summary> /// CreateFile wrapper. Desktop only. Prefer FileManagement.CreateFile() as it will handle all supported platforms. /// </summary> /// <remarks>Not available in Windows Store applications.</remarks> public static SafeFileHandle CreateFile( string path, DesiredAccess desiredAccess, ShareMode shareMode, CreationDisposition creationDisposition, FileAttributes fileAttributes = FileAttributes.NONE, FileFlags fileFlags = FileFlags.NONE, SecurityQosFlags securityQosFlags = SecurityQosFlags.NONE) { uint flags = (uint)fileAttributes | (uint)fileFlags | (uint)securityQosFlags; unsafe { SafeFileHandle handle = Direct.CreateFileW(path, desiredAccess, shareMode, null, creationDisposition, flags, IntPtr.Zero); if (handle.IsInvalid) { throw ErrorHelper.GetIoExceptionForLastError(path); } return(handle); } }