Back


Lennie's Weekly Tip: What's the Drive-Type?
24 April 2000 Ref. 20

Sometimes you may want to determine whether a disk drive is removable, fixed, CD-ROM, RAM disk, or network drive.
To accomplish this you can use the GetDriveType Windows API function.

GETDRIVETYPE
=============

Declaration:
GetDriveType(lpRootPathName : PChar);

Parameters
   lpRootPathName
 Pointer to a null-terminated string that specifies the root directory of the disk to return information about.
 A trailing backslash is required. If lpRootPathName is NULL, the function uses the root of the current directory.

Return Values
   The return value specifies the type of drive. It can be one of the following values:

DRIVE_REMOVABLE
The disk can be removed from the drive.

DRIVE_FIXED
The disk cannot be removed from the drive.

DRIVE_REMOTE
The drive is a remote (network) drive.

DRIVE_CDROM
The drive is a CD-ROM drive.

DRIVE_RAMDISK
The drive is a RAM disk.

The function will return a value of  DRIVE_UNKNOWN if the disk type cannot be determined or a value of
DRIVE_NO_ROOT_DIR if the root path is invalid.

For Example:
-----------------

case GetDriveType(PChar(DriveEdit.Text + ':\')) of
    DRIVE_REMOVABLE: ReportLabel.Caption := 'The disk can be removed from the drive';
    DRIVE_FIXED: ReportLabel.Caption := 'The disk cannot be removed from the drive';
    DRIVE_REMOTE: ReportLabel.Caption := 'The drive is a remote (network) drive';
    DRIVE_CDROM: ReportLabel.Caption := 'The drive is a CD-ROM drive';
    DRIVE_RAMDISK: ReportLabel.Caption := 'The drive is a RAM disk';
  else
    Messagedlg('Error: Drive type cannot be determined or root path is invalid', mterror, [mbok], 0);
  end;
 

This example gets a drive-letter from an edit-box named DriveEdit and passes it to the GetDriveType-function.
The code will automatically add a trailing backslash ( :\ ).
The CASE statement will then report the value returned by GetDriveType-function to a label named ReportLabel.

If GetDriveType doesn't return one of those values eg. it returns either the DRIVE_UNKNOWN or DRIVE_NO_ROOT_DIR, an error message will be displayed.

For a more complete reference to the GetDriveType WinAPI function, goto: http://msdn.microsoft.com/library/psdk/winbase/filesio_2m3p.htm


Back