Get Free Disk Space On Large Drives

This code will allow you get the free disk space on a drive which is over 2 Gb in size. Put all this code into a module

Public Type LargeInt
  lngLower As Long
  lngUpper As Long
End Type

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

Public Function FreeDiskSpace(ByVal sDriveLetter As String) As Double
' Procedure : FreeDiskSpace
' DateTime  : 02/01/2004 12:20
' Author    : Luke
' Purpose   : Returns Free Disk Space using LARGE INT method (Over 2Gb Drives)

Dim udtFreeBytesAvail As LargeInt, udtTtlBytes As LargeInt
Dim udtTTlFree As LargeInt
Dim dblFreeSpace As Double

If GetDiskFreeSpaceEx(sDriveLetter, udtFreeBytesAvail, udtTtlBytes, udtTTlFree) Then
        If udtFreeBytesAvail.lngLower < 0 Then
           dblFreeSpace = udtFreeBytesAvail.lngUpper * 2 ^ 32 + udtFreeBytesAvail.lngLower + 4294967296#
           dblFreeSpace = udtFreeBytesAvail.lngUpper * 2 ^ 32 + udtFreeBytesAvail.lngLower
        End If

End If

FreeDiskSpace = dblFreeSpace

End Function

To use this new module just declare a variable as a double then do this:

Dim dFreeSpace as double

dFreeSpace = FreeDiskSpace("C:\")

Thats it. You will get a value which equals the number of bytes of free space.

You might also like...


Luke Lesurf Erm I went to school, then college and then i got a job... In programming. I like to fly planes and go fishing. I dont like brussle sprouts or cabbage. Love walker's squares. And coke (as in cola)


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan