HOWTO: Use GetDiskFreeSpaceEx to Retrieve Drive Information

ID: Q225144


The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
  • Microsoft Visual Basic for Applications versions 5.0, 6.0


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:

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.


  2. Add the following code to the module for Form1:


  3. 
    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 
  4. 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.


REFERENCES

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
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.