HOWTO: Use GetDiskFreeSpaceEx to Retrieve Drive InformationID: Q225144
|
You can use the Win32 API function GetDiskFreeSpaceEx to determine the number of available bytes, the number of total bytes, and the number of free bytes on a specific drive. This article illustrates how to use the GetDiskFreeSpaceEx function in your Visual Basic project.
The following steps describe how you can create a project that uses the GetDiskFreeSpaceEx function to display the available space, total size, and free space on a specific drive:
Option Explicit
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias _
"GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, _
lpFreeBytesAvailableToCaller As LARGE_INTEGER, lpTotalNumberOfBytes _
As LARGE_INTEGER, lpTotalNumberOfFreeBytes As LARGE_INTEGER) As Long
Private Function CLargeInt(Lo As Long, Hi As Long) As Double
'This function converts the LARGE_INTEGER data type to a double
Dim dblLo As Double, dblHi As Double
If Lo < 0 Then
dblLo = 2 ^ 32 + Lo
Else
dblLo = Lo
End If
If Hi < 0 Then
dblHi = 2 ^ 32 + Hi
Else
dblHi = Hi
End If
CLargeInt = dblLo + dblHi * 2 ^ 32
End Function
Private Sub Form_Click()
Dim lResult As Long
Dim liAvailable As LARGE_INTEGER
Dim liTotal As LARGE_INTEGER
Dim liFree As LARGE_INTEGER
Dim dblAvailable As Double
Dim dblTotal As Double
Dim dblFree As Double
'Determine the Available Space, Total Size and Free Space of a drive
lResult = GetDiskFreeSpaceEx("G:\", liAvailable, liTotal, liFree)
'Convert the return values from LARGE_INTEGER to doubles
dblAvailable = CLargeInt(liAvailable.lowpart, liAvailable.highpart)
dblTotal = CLargeInt(liTotal.lowpart, liTotal.highpart)
dblFree = CLargeInt(liFree.lowpart, liFree.highpart)
'Display the results
Debug.Print "Available Space: " & dblAvailable & " bytes (" & _
Format(dblAvailable / 1024 ^ 3, "0.00") & " G) " & vbCr & _
"Total Space: " & dblTotal & " bytes (" & _
Format(dblTotal / 1024 ^ 3, "0.00") & " G) " & vbCr & _
"Free Space: " & dblFree & " bytes (" & _
Format(dblFree / 1024 ^ 3, "0.00") & " G) "
End Sub
For more information about the GetDiskFreeSpaceEx function, refer to the Platform SDK.
For additional information about using the GetDiskFreeSpace function in Visual Basic, please see the following
article in the Microsoft Knowledge Base:
Q153091 HOWTO: Find and View the Amount of Free Disk Space on a Drive
Additional query words: compatguidestability
Keywords : kbAPI kbWinOS2000 KbVBA kbVBp kbVBp500 kbVBp600 kbVS600 kbWin32s kbGrpDSO
Version : WINDOWS:5.0,6.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: June 16, 1999