Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSee how much disk space is used and free using GetDiskFreeSpaceEx
KeywordsGetDiskFreeSpaceEx, GetDiskFreeSpace, disk space, free, used, precent
CategoriesFiles and Directories, Utilities, Windows, API, Tips and Tricks
 
The GetDiskFreeSpace API function may have problems in Windows 98. GetDiskFreeSpaceEx seems to work.

The cmdGo_Click event handler calls GetDiskFreeSpaceEx to load information into a LARGE_INTEGER structure that has low and high order words. Function LargeIntegerToDouble converts the values into Doubles.

 
Private Sub cmdGo_Click()
Dim bytes_avail As LARGE_INTEGER
Dim bytes_total As LARGE_INTEGER
Dim bytes_free As LARGE_INTEGER
Dim dbl_total As Double
Dim dbl_free As Double

    ' Get the space values.
    GetDiskFreeSpaceEx txtVolume.Text, bytes_avail, _
        bytes_total, bytes_free

    ' Convert values into Doubles.
    dbl_total = LargeIntegerToDouble(bytes_total.lowpart, _
        bytes_total.highpart)
    dbl_free = LargeIntegerToDouble(bytes_free.lowpart, _
        bytes_free.highpart)

    lblTotal.Caption = FormatBytes(dbl_total)
    lblFree.Caption = FormatBytes(dbl_free)
    lblUsed.Caption = FormatBytes(dbl_total - dbl_free)
    lblPercentTotal.Caption = Format$(1, "percent")
    lblPercentFree.Caption = Format$(dbl_free / dbl_total, _
        "percent")
    lblPercentUsed.Caption = Format$((dbl_total - dbl_free) _
        / dbl_total, "percent")
End Sub

' Convert a LARGE_INTEGER structure into
' data type to a Double.
Private Function LargeIntegerToDouble(low_part As Long, _
    high_part As Long) As Double
Dim result As Double

    result = high_part
    If high_part < 0 Then result = result + 2 ^ 32
    result = result * 2 ^ 32

    result = result + low_part
    If low_part < 0 Then result = result + 2 ^ 32

    LargeIntegerToDouble = result
End Function
 
This example also shows how to format large numbers of bytes (KB, MB, GB, etc.). See also Format a BIG number of bytes in KB, MB, GB, TB, etc.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated