首先做準備工作:在FORM1上新建二個LABEL(LABEL1和LABEL2)一個COMMAND1命令按鈕。然后輸入以下代碼:
Private Declare Function GetDriveType Lib
kernel32“Alias "GetDriveTypeA(ByVal nDrive As String) As Long
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 Const DRIVE_FIXED = 3
Private Sub Form_Load() ‘作初始化設置
COMMAND1.Caption = “測試硬盤"
Form1.Caption = “測試硬盤程序"
Label1.WordWrap = True
Label1.Caption = “"
Label2.WordWrap = True
Label2.Caption = “"
End Sub
Private Sub COMMAND1_Click()
Dim DriveNum As Integer
Dim TempDrive As String
Dim X As Long
For DriveNum = 97 To 122 Step 1 ‘檢測從A-Z(盤符)
TempDrive = GetDriveType(Chr(DriveNum) & “:\")
Select Case TempDrive ‘如是3則表示是硬盤,測試你有幾個盤
Case 3: X = GetDiskSpace(Chr(DriveNum)) ‘調用子程序
End Select
Next DriveNum
End Sub
Public Function GetDiskSpace(DrivePath As String)
Dim Drive As String
Dim SectorsPerCluster As Long
Dim BytesPerSector As Long
Dim NumberOfFreeClusters As Long
Dim TotalClusters As Long
Dim Check As Integer
Dim DiskSpace
Dim diskTotal
Static AllDiskTotal As Long
Static NUM As Integer
NUM = NUM + 1 ‘分幾個區的計算
Drive = Left(Trim(DrivePath), 1) & “:\"
Check = GetDiskFreeSpace(Drive, SectorsPerCluster, BytesPerSector, NumberOfFreeClusters, TotalClusters)
If Check <> 0 Then
DiskSpace = SectorsPerCluster * BytesPerSector * NumberOfFreeClusters
‘這是一個分區磁盤剩余空間的計算公式
DiskSpace = Format$(DiskSpace, “###,###") ‘以規定格式顯示,如732,324,231
diskTotal = SectorsPerCluster * BytesPerSector * TotalClusters
‘這是一個分區磁盤總容量的計算公式
diskTotal = Format$(diskTotal, “###,###")
AllDiskTotal = AllDiskTotal + diskTotal ‘整個硬盤的總容量
Label1.Caption =“你的硬盤總容量為:” & Format$(AllDiskTotal,“###,###") &個字節,即:” & Left(AllDiskTotal, 1) & . & Mid(AllDiskTotal, 2, 1) &“G,一共分了”& NUM &“個區,其中:"
Label2.Caption = Label2.Caption & UCase(DrivePath) & “盤的整個容量為:" & diskTotal &“個字節" & ",其剩余磁盤空間為:“& DiskSpace & " 個字節,磁盤已FAT“& SectorsPerCluster & ",每個分區為:“& BytesPerSector & "個字節!埃 vbCrLf & vbCrLf”
End If
End Function
OK!現在你運行一下,你是否滿意它?
注:以上程序在中文WINDOWS98,中文VB6.0企業版中調試通過。
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/