On a computer there are Disk 0, Disk 1,... (as we see in Computer management). These (physical) disks may have partitions or not, mounted or not and so on. I want to know if a Disk with a specific number exists on that computer (no matter what is on that disk). This function has to work fine on any computer with Windows >= XP and it has to be as fast as possible (less than 1 second). A simple enumeration 'A'..'Z' with GetDriveType and DeviceIoControl(...IOCTL_STORAGE_GET_DEVICE_NUMBER..)/DiskSize/DiskFree doesn't help if the disk doesn't have mounted volumes. I'm using Delphi 7.
Use CreateFile()
to open "\\.\PhysicalDriveX"
as the filename, where X
is the desired number, and see if it succeeds or fails. Even if it fails, you can still check if the error is due to the device not being present versus not being accessible (which implies that it exists).