How To Find and View the Amount of Free Disk Space on a Drive

Last reviewed: July 1, 1996
Article ID: Q153091
The information in this article applies to:
  • Professional and Enterprise Editions of Microsoft Visual Basic, 32-bit only, for Windows, version 4.0

SUMMARY

From Visual Basic, it is possible to use the Win32 API function GetDiskFreeSpace to find and view the amount of free disk space on a specified drive. The code sample below shows this in practice.

MORE INFORMATION

Sample Code

  1. Start a new project, Form1 is created by default.

  2. From the File menu, choose Remove File.

  3. From the Insert menu, choose Module.

  4. Add the following code to the Module1 code window:

       Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
       "GetDiskFreeSpaceA"  (ByVal lpRootPathName As String, _
       lpSectorsPerCluster As Long, lpBytesPerSector As Long _
       _lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) _
       As Long
    
       Public Type DiskInformation
           lpSectorsPerCluster As Long
           lpBytesPerSector As Long
           lpNumberOfFreeClusters As Long
           lpTotalNumberOfClusters As Long
       End Type
    
       Sub main()
           Dim info As DiskInformation
           Dim lAnswer As Long
           Dim lpRootPathName As String
           Dim lpSectorsPerCluster As Long
           Dim lpBytesPerSector As Long
           Dim lpNumberOfFreeClusters As Long
           Dim lpTotalNumberOfClusters As Long
           Dim lBytesPerCluster As Long
           Dim lNumFreeBytes As Double
           Dim sString As String
    
           lpRootPathName = "c:\"
           lAnswer = GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, _
           lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters)
           lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector
           lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters
           sString = "Number of Free Bytes : " & lNumFreeBytes & vbCr & vbLf
           sString = sString & "Number of Free Kilobytes: " & (lNumFreeBytes _
              / 1024) & "K" & vbCr & vbLf
           sString = sString & "Number of Free Megabytes: " & _
              Format(((lNumFreeBytes / 1024) / 1024), "0.00") & "MB"
    
           MsgBox sString
       End Sub
    
    

  5. Press the F5 key or choose Start from the Run menu to start the program. You will see a message box displaying the free disk space on drive C:.


Additional reference words: 4.00 vb4win vb432
KBCategory: kbprg kbusage kbhowto
KBSubcategory: APrgOther




THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 1, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.