Microsoft KB Archive/153091

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

Article ID: 153091

Article Last Modified on 3/15/2005

-

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 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q153091



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.



Sample Code
 Start a new project in Visual Basic. Form1 is created by default. Remove Form1 from the project. Add a BAS module to the project.  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

 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 query words: kbVBp400 kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport KBWIN32SDK KBAPI

Keywords: kbhowto KB153091

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.