Direct Disk IO - help needed

Does C# provide any API to do direct disk IO?

I am using CreateFile to do sector-level IO on a compact flash card. The code to
open the drive is as follows:

HANDLE hDevice;

hDevice = CeateFile(TEXT(“\\.\H:”),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);

Here I have hard-coded the drive letter ‘H:’. What if I have some different
drive letter for the removable media on some different computer. The drive
letter will change e.g. may be G, I, K etc. How can I automatically detect the
drive in which the compact flash is inserted. Can anybody help?

> I am using CreateFile to do sector-level IO on a compact flash card. The code to

open the drive is as follows:

HANDLE hDevice;

hDevice = CeateFile(TEXT(“\\.\H:”),…

What does it have to do with direct disk IO??? What you do here is opening a *logical* drive that is mounted either on the logical volume (if the target device pesents itself as a basic disk to the system) or on the partition (if the target device pesents itself as a removable media). In order to deal with the disk itself, you have to specify “\\.\HarddiskX” in CreateFile() call.

What if I have some different drive letter for the removable media on some different computer.

A good question…

Another good question that you can ask yourself is “what if the target machine has more than one keychain attached to it”…

How can I automatically detect the drive in which the compact flash is inserted.

After having asked yourself a question about multiple keychains you will, probably, realize that the above question is just meaningless - you can enumerate disks and drives; you can discover whether
the target drive is mounted on the removable media or on the basic disk (in latter case you can also
discover the bus type so that you can make a distinction between the hard disks and USB keychains that present themselves as ones), but still you need some signature that allows you to tell your target storage device from other USB storage devices that may be attached to the target machine…

Anton Bassov

>In order to deal with the disk itself, you have to specify “\\.\HarddiskX” in CreateFile() call.

A correction to my statement above is needed - what you have to specify in CreateFile() call is
“\\.\PhysicalDriveX” - “\Device\HarddiskX\DRn” is the actual name that is used by the system,
but CreateFile() expects not the actual name but a symbolic link in the form “\\.\PhysicalDriveX”…

Anton Bassov