Microsoft KB Archive/225144

= HOWTO: Use GetDiskFreeSpaceEx to Retrieve Drive Information =

Article ID: 225144

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic for Applications 5.0
 * Microsoft Visual Basic for Applications 6.0

-



This article was previously published under Q225144



SUMMARY
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.



MORE INFORMATION
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:  Start a new Standard EXE project in Visual Basic. Form1 is created by default.  Add the following code to the module for Form1: 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  Press the F5 key to run the program and click the Form.

Results: The drive information is written to the immediate window of the Visual Basic IDE.

Additional Note
You can use the GetDiskFreeSpaceEx function with the following versions of Windows:
 * Windows 95 beginning with OEM Service Release 2 (OSR2)
 * Windows NT 3.51 and later
 * Windows 98
 * Windows 2000

If you are using a Windows 95 version prior to OSR2, you can use the GetDiskFreeSpace function. However, this function does not return byte values that exceed 2 gigabytes.

