Esempio n. 1
0
        public static bool UnmountVolume(SafeFileHandle volumeHandle)
        {
            uint bytesReturned;
            var  result = NativeInterop.DeviceIoControl(volumeHandle, NativeInterop.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);

            return(result);
        }
Esempio n. 2
0
        public static SafeFileHandle GetVolumeHandle(string drive, bool readOnly = true)
        {
            var path   = $"\\\\.\\{drive.TrimEnd('\\')}".ToUpperInvariant();
            var handle = NativeInterop.CreateFile(path, (readOnly ? FileAccess.Read : FileAccess.ReadWrite), FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);

            if (handle.IsInvalid)
            {
                handle.Dispose();

                return(null);
            }

            return(handle);
        }
Esempio n. 3
0
        public static SafeFileHandle GetDeviceHandle(ushort diskNumber, bool readOnly = true)
        {
            var path   = $"\\\\.\\PhysicalDrive{diskNumber}";
            var handle = NativeInterop.CreateFile(path, (readOnly ? FileAccess.Read : FileAccess.ReadWrite), FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);

            if (handle.IsInvalid)
            {
                handle.Dispose();

                return(null);
            }

            return(handle);
        }
Esempio n. 4
0
        public static bool GetDiskGeometry(ushort diskNumber, out NativeInterop.DISK_GEOMETRY geometry)
        {
            geometry = new NativeInterop.DISK_GEOMETRY();

            var deviceHandle = GetDeviceHandle(diskNumber);

            if (deviceHandle == null)
            {
                return(false);
            }

            using (deviceHandle)
            {
                var geometrySize = 0U;

                return(NativeInterop.DeviceIoControl(deviceHandle, NativeInterop.IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, ref geometry, (uint)Marshal.SizeOf(geometry), out geometrySize, IntPtr.Zero));
            }
        }
Esempio n. 5
0
        public static ushort[] GetPhysicalDiskNumbersFromDrive(string drive)
        {
            if (string.IsNullOrWhiteSpace(drive))
            {
                return(new ushort[0]);
            }

            var diskNumbers  = new List <uint>();
            var volumeHandle = GetVolumeHandle(drive);

            if (volumeHandle == null)
            {
                return(new ushort[0]);
            }

            using (volumeHandle)
            {
                var diskExtents     = new NativeInterop.VOLUME_DISK_EXTENTS();
                var diskExtentsSize = 0U;
                var success         = NativeInterop.DeviceIoControl(volumeHandle, NativeInterop.IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, IntPtr.Zero, 0, ref diskExtents, (uint)Marshal.SizeOf(diskExtents), out diskExtentsSize, IntPtr.Zero);

                if (!success || Utils.IsNullOrEmpty(diskExtents.Extents))
                {
                    return(new ushort[0]);
                }

                for (var i = 0; i < diskExtents.NumberOfDiskExtents; i++)
                {
                    diskNumbers.Add(diskExtents.Extents[i].DiskNumber);
                }

                return(diskNumbers.Distinct()
                       .Select(Convert.ToUInt16)
                       .OrderBy(_ => _)
                       .ToArray());
            }
        }
Esempio n. 6
0
        public static bool UnlockVolume(SafeFileHandle volumeHandle)
        {
            uint bytesReturned;

            return(NativeInterop.DeviceIoControl(volumeHandle, NativeInterop.FSCTL_UNLOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero));
        }