Determining free Disk Space VB Help

This section describes the functions you use to determine a drive’s medium type and its free disk space and the functions you use to find the Windows directory arid the current directory; they are:

  1. GetDriveType()
  2. GetDiskFreeSpace()
  3. GetWindowsDirectory()
  4. GetCurrentl?irectory()

These functions add capabilities that are normally not available with Visual Basic. For example, you can use them to determine the amount of free space on a disk drive or if a certain drive is a CD-ROM drive.


This function determines the drive type and is declared as follows:

Private Declare Function GetDr;veType Lib ‘kern~32′ Alias _
“GetDr;veTypeA’ (ByVal nDrive As String) As Long

You pass the drive that you want to check with the nDrive argument and the function returns a Long value, which describes the type of-the drive. Table 13.2 lists and describes the values.

TABLE 13 .2

TABLE 13 .2

VB6 at Work: The Drives Project

The Drives application (see Figure 13.2) displays information about the selected drive on the DriveListBox, as well as the current folder and the Windows folder on the system. Every time you select a different drive, the information on the application’s Form is updated. The Drives application uses three API functions: GetDiskf.reeSpace(), GetCurrent Directory(), and GetWindowsDirectory()




This function gets specific information about a disk. including the amount Of free space. It also returns the number of sectors per cluster, the number of bytes per sector, the total number of free clusters on the disk, and the total number of clusters. The free disk space isn’t reported directly by the GetDiskFreeSpace() function; instead, it must be calculated by taking the product of the bytes per sector, sectors per cluster, and the number of free clusters. Here’s how you declare the function:

The arguments of this function have intuitive names, and you’ll see how they are used in the File Info application, later in this chapter.

To find out the free space on drive C, use the following statements:

The total free space is calculated as the product of free clusters, sectors per cluster, and bytes per sector.

GetCurrent Directory()

Use this function to retrieve the current directory where your program is running; it’s similar to ~ App.Path property ofVlSual Basic.This function expects two arguments: the buffer length and the buffer in which. the pathname will be stored. The function is declared as follows:

When the function returns, you can read the value of the IpBuffer argument to find out the current path.


Use this function to find where Windows is installed on the hard drive. You may need this information to install any initialization files or help files in the widows directory. Its arguments are identical to the arguments of the GetCurrentDirectory() function, and it’s declared as follows:

The complete listing of the Drives project is shown next.

The Drives Project
Code 13.3:

Other File Functions

Sometimes you want information about a file such as-its path, attributes, or size. The FileInfo application on the CD (see Figure 13.3) demonstrates’ how ‘to retrieve’ information about ‘a file using API functions. ‘



VB6 at Work: The Filelnfo Project

This FileInfo application uses the following functions:

  1. GetFullPathName()
  2. GetFileAttributes()
  3. CetFileSize()

GetFullPathName() This function obtains the full path name of a file, and its declaration is as follows:

In the FileInfo application, the GetFullPathName() function returns the path for the file selected by the user in a standard File Open dialog box. It accepts as an argument the name of the file whose full path you want an~ returns the path of the file-: name in the filePath variable.

GetFileAttributes() This function returns a flag that indicates the status of the file: read-only, hidden, normal, and so on. It’s similar to the Visual Basic Get- FileAttributes() function, and its declaration is as follows:

Private Declare Function GetfileAttributes lib “kernel32” Alias
“GetFileAttributes” (ByVal lpFileName As String) As long

Table 1.33 lists the attributes that a file can have. The FileInfo application demonstrates the use of this function.

TABLE 13.3

TABLE 13.3

GetFileSlze() To determine the file size, first open the file using, the Create- File() function (described previously in the section “Managing Large Data Files”) with the OPEN EXISTING flag to make sure the file already exists. )’ou can now use the GetFileSize() function to return the file size in bytes. Of course, you must close the file with the CloseHandle() function when you’re done. .

The complete listining of the FileInfo project is shown next. ,

The FileInfo Project

Posted on November 5, 2015 in The Windows API

Share the Story

Back to Top
Share This