/// <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); }
//[Test] public void AsyncReadWriteTest() { var fn = Path.GetTempFileName(); uint sz = 1024 * 1024 * 128; using (var hFile = CreateFile(fn, Kernel32.FileAccess.GENERIC_WRITE, FileShare.Write, null, FileMode.OpenOrCreate, FileFlagsAndAttributes.FILE_FLAG_OVERLAPPED, IntPtr.Zero)) { var bytes = GetBigBytes(sz, 1); Assert.That(bytes[1024], Is.EqualTo(1)); new TaskFactory().FromAsync <HFILE, byte[], uint>(BeginWriteFile, EndWriteFile, hFile, bytes, (uint)bytes.Length, 1).Wait(); } var fi = new FileInfo(fn); Assert.That(fi.Exists); Assert.That(fi.Length, Is.EqualTo(sz)); var cfp2 = new CREATEFILE2_EXTENDED_PARAMETERS { dwFileFlags = FileFlagsAndAttributes.FILE_FLAG_OVERLAPPED, dwSize = (uint)Marshal.SizeOf(typeof(CREATEFILE2_EXTENDED_PARAMETERS)) }; using (var hFile = CreateFile2(fn, Kernel32.FileAccess.GENERIC_READ, FileShare.Read, FileMode.Open, cfp2)) { var bytes = GetBigBytes(sz); Assert.That(bytes[1024], Is.EqualTo(0)); new TaskFactory().FromAsync <HFILE, byte[], uint>(BeginReadFile, EndReadFile, hFile, bytes, (uint)bytes.Length, 1).Wait(); Assert.That(bytes[1024], Is.EqualTo(1)); } Assert.That(DeleteFile(fn), Is.True); }
/// <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> /// 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); }
public static extern IntPtr CreateFile2(string lpFileName, uint dwDesiredAccess, uint dwShareMode, uint dwCreationDisposition, CREATEFILE2_EXTENDED_PARAMETERS pCreateExParams);
internal static extern SafeFileHandle CreateFile2( string lpFileName, int dwDesiredAccess, System.IO.FileShare dwShareMode, System.IO.FileMode dwCreationDisposition, [In] ref CREATEFILE2_EXTENDED_PARAMETERS parameters);
internal static extern unsafe IntPtr CreateFile2( string lpFileName, int dwDesiredAccess, FileShare dwShareMode, FileMode dwCreationDisposition, [In] ref CREATEFILE2_EXTENDED_PARAMETERS parameters);
private static extern SafePipeHandle CreateNamedPipeClientPrivate( string lpFileName, int dwDesiredAccess, System.IO.FileShare dwShareMode, System.IO.FileMode dwCreationDisposition, [In] ref CREATEFILE2_EXTENDED_PARAMETERS parameters);
public static extern SafeFileHandle CreateFile2( string lpFileName, DesiredAccess dwDesiredAccess, ShareModes dwShareMode, CreationDisposition dwCreationDisposition, [In] ref CREATEFILE2_EXTENDED_PARAMETERS pCreateExParams);