示例#1
0
 private static extern bool DeviceIoControl(
     SafeFileHandle hDevice,
     uint dwIoControlCode,
     IntPtr lpInBuffer,
     uint nInBufferSize,
     ref VolumeDiskExtents lpOutBuffer,
     uint nOutBufferSize,
     out uint lpBytesReturned,
     IntPtr lpOverlapped);
示例#2
0
        /// <summary>
        ///     Gets the device ID by drive letter.
        /// </summary>
        /// <param name="driveLetter">A valid drive letter.</param>
        /// <returns>The device ID.</returns>
        /// <exception cref="DetectionFailedException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        private static int GetDiskId(char driveLetter)
        {
            var di = new DriveInfo(driveLetter.ToString());

            if (di.DriveType != DriveType.Fixed)
            {
                throw new DetectionFailedException(string.Format("This drive is not fixed drive: {0}", driveLetter));
            }

            var sDrive = "\\\\.\\" + driveLetter + ":";

            var hDrive = CreateFileW(
                sDrive,
                0, // No access to drive
                FileShareRead | FileShareWrite,
                IntPtr.Zero,
                OpenExisting,
                FileAttributeNormal,
                IntPtr.Zero);

            if (hDrive == null || hDrive.IsInvalid)
            {
                throw new DetectionFailedException(string.Format("Could not detect Disk Id of {0}",
                                                                 driveLetter));
            }

            var ioctlVolumeGetVolumeDiskExtents = CTL_CODE(
                IoctlVolumeBase, 0,
                MethodBuffered, FileAnyAccess); // From winioctl.h

            var queryDiskExtents =
                new VolumeDiskExtents();

            uint returnedQueryDiskExtentsSize;

            var queryDiskExtentsResult = DeviceIoControl(
                hDrive,
                ioctlVolumeGetVolumeDiskExtents,
                IntPtr.Zero,
                0,
                ref queryDiskExtents,
                (uint)Marshal.SizeOf(queryDiskExtents),
                out returnedQueryDiskExtentsSize,
                IntPtr.Zero);

            hDrive.Close();

            if (queryDiskExtentsResult == false ||
                queryDiskExtents.Extents.Length != 1)
            {
                throw new DetectionFailedException(string.Format("Could not detect Disk Id of {0}",
                                                                 driveLetter));
            }

            return((int)queryDiskExtents.Extents[0].DiskNumber);
        }
示例#3
0
        private static int GetDiskNumber(string drive)
        {
            var diskNumber = -1;
            var deviceName = GetDeviceName(drive);

            if (deviceName != null)
            {
                using (var vde = new VolumeDiskExtents())
                {
                    if (vde.Load(deviceName))
                    {
                        var diskExtents = vde.GetDiskExtents();
                        if (diskExtents.Count == 1)
                        {
                            diskNumber = diskExtents[0].DiskNumber;
                        }
                    }
                }
            }

            return(diskNumber);
        }
示例#4
0
        /// <summary>
        ///     Gets the device ID by drive letter.
        /// </summary>
        /// <param name="driveLetter">A valid drive letter.</param>
        /// <returns>The device ID.</returns>
        /// <exception cref="DetectionFailedException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        private static int GetDiskId(char driveLetter)
        {
            var di = new DriveInfo(driveLetter.ToString());
            if (di.DriveType != DriveType.Fixed)
            {
                throw new DetectionFailedException(string.Format("This drive is not fixed drive: {0}", driveLetter));
            }

            var sDrive = "\\\\.\\" + driveLetter + ":";

            var hDrive = CreateFileW(
                sDrive,
                0, // No access to drive
                FileShareRead | FileShareWrite,
                IntPtr.Zero,
                OpenExisting,
                FileAttributeNormal,
                IntPtr.Zero);

            if (hDrive == null || hDrive.IsInvalid)
            {
                throw new DetectionFailedException(string.Format("Could not detect Disk Id of {0}",
                    driveLetter));
            }

            var ioctlVolumeGetVolumeDiskExtents = CTL_CODE(
                IoctlVolumeBase, 0,
                MethodBuffered, FileAnyAccess); // From winioctl.h

            var queryDiskExtents =
                new VolumeDiskExtents();

            uint returnedQueryDiskExtentsSize;

            var queryDiskExtentsResult = DeviceIoControl(
                hDrive,
                ioctlVolumeGetVolumeDiskExtents,
                IntPtr.Zero,
                0,
                ref queryDiskExtents,
                (uint) Marshal.SizeOf(queryDiskExtents),
                out returnedQueryDiskExtentsSize,
                IntPtr.Zero);

            hDrive.Close();

            if (queryDiskExtentsResult == false ||
                queryDiskExtents.Extents.Length != 1)
            {
                throw new DetectionFailedException(string.Format("Could not detect Disk Id of {0}",
                    driveLetter));
            }

            return (int) queryDiskExtents.Extents[0].DiskNumber;
        }
示例#5
0
 private static extern bool DeviceIoControl(
     SafeFileHandle hDevice,
     uint dwIoControlCode,
     IntPtr lpInBuffer,
     uint nInBufferSize,
     ref VolumeDiskExtents lpOutBuffer,
     uint nOutBufferSize,
     out uint lpBytesReturned,
     IntPtr lpOverlapped);