Determining drive types in Visual FoxPro

An easy way to enumerate the different types of disk drives installed on an end-user's computer.

By Mike Lewis

1No such drive
2Removable drive (e.g. floppy, USB stick)
3Fixed drive
4Network drive
5CD-ROM (or DVD) drive
6RAM disk

Table 1: Values returned by GetDriveType.

When writing VFP code, you sometimes need to know which drive letters are available on the user's system - and which types of drives they refer to. For example, if you're creating a file in a user-specified location, you might need to know if the location is a hard drive, a network drive, or a removable device such as a USB stick.

In fact, it's very easy to get this information. With just a few lines of code, you can programmatically enumerate the drives on a user's system, and determine which category each drive belongs to.

The trick is to use a simple Windows API function, appropriately named GetDriveType(). The function takes a single parameter: a drive letter followed by a colon. If the drive doesn't exist, the function returns 1. Otherwise, it returns an integer between 2 and 6 (see Table 1). A reply of zero means that the drive type can't be determined.

As an example, the following code tells you if Drive E is a fixed drive:

DECLARE INTEGER GetDriveType IN kernel32 STRING cDriveLetter

IF GetDriveType("E:") = 3
  ? "Drive E is a fixed drive"
  ? "Drive E is not a fixed drive"

As with all API calls, you must declare the function before you use it for the first time. That's what the DECLARE statement in the above code does. You only need to do that once within the session, although no harm is done if you do it multiple times (apart from a very small consumption of resources).

By the way, when testing a floppy or CD-ROM drive, the function returns the correct result regardless of whether or not a disk is present in the drive. But that's not the case with a USB stick. If the stick isn't plugged into the port, the function will (correctly) report that no drive is present.

For another example, this code will enumerate all the drives on the system:

LOCAL lnType, lcDrive, lnI

DECLARE INTEGER GetDriveType IN kernel32 STRING cDriveLetter

FOR lnI = 0 TO 25
  lcDrive = CHR(65 + lnI) + ":"
  lnType = GetDriveType(lcDrive)
  IF lnType <> 1
    ? lcDrive + SPACE(5) + ICASE( ;
       lnType = 2, "Removable", ;
       lnType = 3, "Fixed", ;
       lnType = 4, "Network", ;
       lnType = 5, "CD-ROM", ;
       lnType = 6, "RAM Disk", "Unknown")

Note that the ICASE() function in the above code is only available in VFP 9.0. If you're using an earlier FoxPro version, use a nested IF / ENDIF or a DO CASE / ENDCASE construct instead.

If you're not comfortable using Windows API calls from within VFP, you have another option. The Foxtools library contains a function named DriveType() which works in almost exactly the same way. To use it, just change the DECLARE command to a SET LIBRARY command, and call DriveType() instead of GetDriveType(). The main disadvantage of this approach is that you will need to ensure that FOXTOOLS.FLL is available to your application at run time.

December 2012

Please note: The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly with your sites or applications.

If you found this article useful, please tell your friends: