GetDiskFreeSpaceEx Function

Declare Function GetDiskFreeSpaceEx Lib "kernel32.dll" Alias "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, lpFreeBytesAvailableToCaller As ULARGE_INTEGER, lpTotalNumberOfBytes As ULARGE_INTEGER, lpTotalNumberOfFreeBytes As ULARGE_INTEGER) As Long

Platforms

Description & Usage

GetDiskFreeSpaceEx determines information about the size of a disk. It finds the free space available to the current user, the total disk space, and the amount of free space (all in bytes). Each value is placed into a ULARGE_INTEGER structure which can hold the unsigned 64-bit integer values. (If your programming language supplies an intrinsic unsigned 64-bit integer data type, that can be used instead.)

Return Value

If an error occured, the function returns 0 (use GetLastError to retrieve the error code). If successful, the function returns a non-zero value.

Visual Basic-Specific Issues

None.

Parameters

lpDirectoryName
The name of a directory on the disk to retrieve the size information about. While this can be the name of the disk's root directory, it doesn't have to be.
lpFreeBytesAvailableToCaller
Unsigned 64-bit integer variable which receives the amount of free disk space available, in bytes, to the user. Windows 2000: This may be lower than the actual value if per-user disk space quotas are enabled.
lpTotalNumberOfBytes
Unsigned 64-bit integer variable which receives the amount of total disk space, in bytes. Windows 2000: This may be lower than the actual value if per-user disk space quotas are enabled.
lpTotalNumberOfFreeBytes
Unsigned 64-bit integer variable which receives the amount of free disk space, in bytes.

Example

Display the total free space available on drive C:. Because Visual Basic doesn't have good support for the 64-bit integers needed for modern hard drive's free spaces, the workaround described on this page is needed to display the values properly. This example runs when the user click button Command1, so to use this example, you need to place a command button named Command1 on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type ULARGE_INTEGER
	LowPart As Long
	HighPart As Long
End Type
Public Declare Function GetDiskFreeSpaceEx Lib "kernel32.dll" Alias "GetDiskFreeSpaceExA" (ByVal _
	lpDirectoryName As String, lpFreeBytesAvailableToCaller As ULARGE_INTEGER, _
	lpTotalNumberOfBytes As ULARGE_INTEGER, lpTotalNumberOfFreeBytes As ULARGE_INTEGER) As Long
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _
	As Any, ByVal Length As Long)

' *** Place the following code inside the form window. ***

Private Sub Command1_Click()
	Dim userbytes As ULARGE_INTEGER   ' bytes free to user
	Dim totalbytes As ULARGE_INTEGER  ' total bytes on disk
	Dim freebytes As ULARGE_INTEGER   ' free bytes on disk
	Dim tempval As Currency           ' display buffer for 64-bit values
	Dim retval As Long                ' generic return value
	
	' Get information about the C: drive.
	retval = GetDiskFreeSpaceEx("C:\", userbytes, totalbytes, freebytes)
	' Copy freebytes into the Currency data type.
	CopyMemory tempval, freebytes, 8
	' Multiply by 10,000 to move Visual Basic's decimal point to the end of the actual number.
	tempval = tempval * 10000
	' Display the amount of free space on C:.
	Debug.Print "Free Space on the C: drive:"; tempval; "bytes"
End Sub

See Also

GetDiskFreeSpace

Category

Files

Go back to the Function listing.
Go back to the Reference section index.


Last Modified: September 24, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getdiskfreespaceex.html