Exemple #1
0
        /// <summary>
        /// Sends the FSCTL_GET_NTFS_VOLUME_DATA control code, returning the resuling
        /// NTFS_VOLUME_DATA_BUFFER.
        /// </summary>
        /// <param name="volume">The volume to query.</param>
        /// <returns>The NTFS_VOLUME_DATA_BUFFER structure representing the data
        /// file system structures for the volume, or null if the data could not be
        /// retrieved.</returns>
        internal static NativeMethods.NTFS_VOLUME_DATA_BUFFER GetNtfsVolumeData(VolumeInfo volume)
        {
            using (FileStream stream = volume.Open(FileAccess.Read, FileShare.ReadWrite,
                                                   FileOptions.None))
                using (SafeFileHandle handle = stream.SafeFileHandle)
                {
                    uint resultSize = 0;
                    NativeMethods.NTFS_VOLUME_DATA_BUFFER volumeData =
                        new NativeMethods.NTFS_VOLUME_DATA_BUFFER();
                    if (NativeMethods.DeviceIoControl(handle,
                                                      NativeMethods.FSCTL_GET_NTFS_VOLUME_DATA, IntPtr.Zero, 0, out volumeData,
                                                      (uint)Marshal.SizeOf(volumeData), out resultSize, IntPtr.Zero))
                    {
                        return(volumeData);
                    }

                    throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
                }
        }
Exemple #2
0
 internal static NativeMethods.NTFS_VOLUME_DATA_BUFFER GetNtfsVolumeData(VolumeInfo volume)
 {
     using (SafeFileHandle volumeHandle = NativeMethods.CreateFile(
     volume.VolumeId.Remove(volume.VolumeId.Length - 1),
     NativeMethods.GENERIC_READ, NativeMethods.FILE_SHARE_READ |
     NativeMethods.FILE_SHARE_WRITE, IntPtr.Zero, NativeMethods.OPEN_EXISTING,
     0, IntPtr.Zero))
        {
     uint resultSize = 0;
     NativeMethods.NTFS_VOLUME_DATA_BUFFER volumeData =
      new NativeMethods.NTFS_VOLUME_DATA_BUFFER();
     if (NativeMethods.DeviceIoControl(volumeHandle,
      NativeMethods.FSCTL_GET_NTFS_VOLUME_DATA, IntPtr.Zero, 0, out volumeData,
      (uint)Marshal.SizeOf(volumeData), out resultSize, IntPtr.Zero))
     {
      return volumeData;
     }
     throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
        }
 }