Private 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
Private Sub Command1_Click()
Dim a, b, c, d, free, total As Long
Dim disk As String
disk = Text1.Text
GetDiskFreeSpace disk, a, b, c, d
'计算磁盘总容量
total = d * b * a / 1024 / 1024
'计算磁盘自由空间
free = c * a * b / 1024 / 1024
'显示磁盘总容量与自由空间
LabT.Caption = total & "MB"
LabF.Caption = free & "MB"
End Sub
' API声明
Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long
Private Sub Form_Load()
GetDriveInfo
End Sub
' 选中一个驱动器时
Private Sub drvSelect_Change()
GetDriveInfo
End Sub
' 获取磁盘剩余空间信息并显示在相应的标签中
Private Function GetDriveInfo()
Dim sDrive As String
Dim lBytes As Long
Dim lMBFree As Double
Dim lMBTotal As Double
Dim lSecPerClust As Long ' 扇区每簇
Dim lBytePerSect As Long ' 字节每扇区
Dim lNumFreeClust As Long ' 空白簇的数目
Dim lTotalNumClust As Long ' 簇的总数