Help & Manual authoring tool
The IsDiskIn function presented here determines whether a removable-disk unit is empty or not

Determining if there is a disk/diskette/CD in a removable-disk drive

Copyright © 2000 Ernesto De Spirito

Storage Library - Save your app's configuration

IsDiskIn

The trick is done by calling the API GetDiskFreeSpace and returning its return value as a boolean. Here is the source code of a function that 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:

uses Windows;

var
  DrivePath: array [0..3] of char = 'A:\';

function IsDiskIn(drive: char): boolean;
var
  d1, d2, d3, d4: longword;
begin
  DrivePath[0] := drive;
  Result := GetDiskFreeSpace(DrivePath, d1, d2, d3, d4);
end;

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.

Sample call

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not IsDiskIn('A') then
    ShowMessage('Drive A: Not Ready');
end;
JfControls Library - for Delphi and C++ Builder
Copyright © 2000/2006 Ernesto De Spirito.   All rights reserved.