Determining if there is a disk/diskette/CD in a removable-disk drive (Views: 100)
How can I know if there is a CD in the CD drive?
The trick is done by calling the API GetDiskFreeSpace and returning its return value as a boolean. The following function takes the drive letter as a parameter (for example 'A', 'D', etc.) and returns True if there is a disk in the drive, or False if not.
DrivePath: array[0..3] of char = 'A:\';
function IsDiskIn(drive: char): boolean;
d1, d2, d3, d4: longword;
DrivePath := drive;
Result := GetDiskFreeSpace(DrivePath, d1, d2, d3, d4);
In the implementation we use an initialized null-terminated string (DrivePath) that contains the root directory of drive A: and we substitute the drive letter with the one passed as parameter before calling GetDiskFreeSpace.
procedure TForm1.Button1Click(Sender: TObject);
if not IsDiskIn('A') then
ShowMessage('Drive A: Not Ready');
Copyright (c) 2001 Ernesto De Spirito
<< Back to main page