Search code examples
c#filedisk

Determine what physical hard drive a file is on?


How can I find the physical drive a file/directory is located on. I want to compare 2 files and if they are on two separate drives, then I can speed up the compare by reading both simultaneously.

Regards


Solution

  • This may require P/Invoke.

    Way to know if two partitions are in one physical hard disk without WMI?

    From above link:

    The Win32 function you'll need is called DeviceIoControl(). The API documentation can be found at http://msdn.microsoft.com/en-us/library/aa363216(VS.85).aspx. Call DeviceIoControl() with the control code IOCTL_STORAGE_GET_DEVICE_NUMBER and you'll get the physical disk drive for the given partition device handle. The device handle for the partition can be retrieved using CreateFile() API.