internal static SafeFileHandle CreateFile( string path, FileAccess fileAccess, FileShare fileShare, FileMode creationDisposition, AllFileAttributeFlags flagsAndAttributes) { path = Paths.AddExtendedPrefix(path); if (creationDisposition == FileMode.Append) { creationDisposition = FileMode.OpenOrCreate; } uint dwDesiredAccess = ((fileAccess & FileAccess.Read) != 0 ? Private.GENERIC_READ : 0) | ((fileAccess & FileAccess.Write) != 0 ? Private.GENERIC_WRITE : 0); SafeFileHandle handle = Private.CreateFileW(path, dwDesiredAccess, fileShare, IntPtr.Zero, creationDisposition, flagsAndAttributes, IntPtr.Zero); if (handle.IsInvalid) { int error = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(error, path); } return(handle); }
internal static extern SafeFileHandle CreateFileW( string lpFileName, uint dwDesiredAccess, [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, IntPtr lpSecurityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition, AllFileAttributeFlags dwFlagsAndAttributes, IntPtr hTemplateFile);
internal static string GetFinalPathName(string path, FinalPathFlags finalPathFlags, bool resolveLinks) { if (path == null) { return(null); } string lookupPath = Paths.AddExtendedPrefix(path); // BackupSemantics is needed to get directory handles AllFileAttributeFlags createFileFlags = AllFileAttributeFlags.FILE_ATTRIBUTE_NORMAL | AllFileAttributeFlags.FILE_FLAG_BACKUP_SEMANTICS; if (!resolveLinks) { createFileFlags |= AllFileAttributeFlags.FILE_FLAG_OPEN_REPARSE_POINT; } string finalPath = null; using (SafeFileHandle file = CreateFile( lookupPath, // To look at metadata we don't need read or write access 0, FileShare.ReadWrite, FileMode.Open, createFileFlags)) { if (file.IsInvalid) { int error = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(error, path); } finalPath = NativeMethods.BufferInvoke((buffer) => Private.GetFinalPathNameByHandleW(file, buffer, (uint)buffer.CharCapacity, finalPathFlags), path); } // GetFinalPathNameByHandle will use the legacy drive for the volume (e.g. \\?\C:\). We may have started with \\?\Volume({GUID}) or some // other volume name format (C:\, etc.) we want to put it back. return(Paths.ReplaceRoot(path, finalPath)); }
internal static SafeFileHandle CreateFile( string path, FileAccess fileAccess, FileShare fileShare, FileMode creationDisposition, AllFileAttributeFlags flagsAndAttributes) { path = Paths.AddExtendedPrefix(path); if (creationDisposition == FileMode.Append) creationDisposition = FileMode.OpenOrCreate; uint dwDesiredAccess = ((fileAccess & FileAccess.Read) != 0 ? Private.GENERIC_READ : 0) | ((fileAccess & FileAccess.Write) != 0 ? Private.GENERIC_WRITE : 0); SafeFileHandle handle = Private.CreateFileW(path, dwDesiredAccess, fileShare, IntPtr.Zero, creationDisposition, flagsAndAttributes, IntPtr.Zero); if (handle.IsInvalid) { int error = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(error, path); } return handle; }
private static extern SafeFileHandle CreateFilePrivate( string fileName, [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, [MarshalAs(UnmanagedType.U4)] FileShare fileShare, IntPtr securityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, AllFileAttributeFlags flagsAndAttributes, IntPtr template);
internal static SafeFileHandle CreateFile( string path, FileAccess fileAccess, FileShare fileShare, FileMode creationDisposition, AllFileAttributeFlags flagsAndAttributes) { path = Paths.AddExtendedPathPrefix(path); if (creationDisposition == FileMode.Append) creationDisposition = FileMode.OpenOrCreate; SafeFileHandle handle = CreateFilePrivate(path, fileAccess, fileShare, IntPtr.Zero, creationDisposition, flagsAndAttributes, IntPtr.Zero); if (handle.IsInvalid) { int error = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(error, path); } return handle; }