CodeItBetter Programming Another VB Programming Blog

How to get free disk space of C drive (in two ways)

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'System & API - How to get free disk space of C drive (in two ways)
Method 1:
 
Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" _
    (ByVal lpDirectoryName As String, lpFreeBytesAvailable As Currency, _
    lpTotalNumberOfBytes As Currency, lpTotalNumberOfFreeBytes As Currency) As Long
 
Private Function DetermineFreeSpace(strDrive As String) As Double
    Dim BytesFree As Currency, TotBytes As Currency, TotFreeBytes As Currency
    Call GetDiskFreeSpaceEx(strDrive, BytesFree, TotBytes, TotFreeBytes)
    DetermineFreeSpace = (BytesFree * 10000) / 1024 ^ 3
End Function
 
Private Sub Command1_Click()
    MsgBox "Free space : " & Format(CStr(DetermineFreeSpace("C:")), "##0.0") & " GB"
End Sub
 
Method 2:
 
Public Sub CalculateFreeSpace()
    Dim oFSO As Object
    Dim oDrive As Object
 
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oDrive = oFSO.GetDrive("C:")
 
    If oDrive.IsReady Then
        MsgBox "Free space: " & ConvertFreeSpace(oDrive.FreeSpace)
    Else
        MsgBox "Free space: <drive unavailable>"
    End If
 
    Set oDrive = Nothing
    Set oFSO = Nothing
End Sub
 
Private Function ConvertFreeSpace(ByVal cFreeSpace As Currency) As String
    Select Case cFreeSpace
        Case Is < 1024
            ConvertFreeSpace = cFreeSpace & " bytes"
        Case Is < 1024 ^ 2
            ConvertFreeSpace = FormatNumber(cFreeSpace / 1024, 2) & " KB"
        Case Is < 1024 ^ 3
            ConvertFreeSpace = FormatNumber(cFreeSpace / 1024 ^ 2, 2) & " MB"
        Case Else
            ConvertFreeSpace = FormatNumber(cFreeSpace / 1024 ^ 3, 2) & " GB"
    End Select
End Function
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.