Beispiel #1
0
 private static extern SafeFileHandle _OpenFileById(
     [In] SafeFileHandle hVolumeHint,
     [In, Out] ref FILE_ID_DESCRIPTOR lpFileId,
     [In, MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
     [In, MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
     [In] IntPtr lpSecurityAttributes,
     [In, MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes
     );
Beispiel #2
0
 private static SafeFileHandle _OpenFileById(SafeFileHandle hint, ref FILE_ID_DESCRIPTOR fileId) =>
 _OpenFileById(
     hint,
     ref fileId,
     FileAccess.ReadWrite,
     FileShare.ReadWrite,
     IntPtr.Zero,
     _FILE_FLAG_BACKUP_SEMANTICS);
Beispiel #3
0
        /// <inheritdoc />

        public static string GetFilePath(long fileSystemId)
        {
            using (var handle = _CreateSafeFileHandle("."))
            {
                if (handle == null || handle.IsInvalid)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                var size       = Marshal.SizeOf(typeof(FILE_ID_DESCRIPTOR));
                var descriptor = new FILE_ID_DESCRIPTOR {
                    Type = FILE_ID_TYPE.FileIdType, FileId = fileSystemId, dwSize = size
                };

                return(GetFinalPath(handle, descriptor));
            }
        }
Beispiel #4
0
        private static string GetFinalPath(SafeFileHandle handle, FILE_ID_DESCRIPTOR desc)
        {
            try
            {
                using (var handle2 = _OpenFileById(handle, ref desc))
                {
                    if (handle2 == null || handle2.IsInvalid)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }

                    const int length  = 128;
                    var       builder = new StringBuilder(length);
                    _GetFinalPathNameByHandleW(handle2, builder, length, 0);
                    return(builder.ToString());
                }
            }
            catch
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
            return(null);
        }