本例子中,我们借助FileSystemObject对象的DriveExists属性,可以检查获取磁盘的容量信息。
首先弹出输入对话框,提示输出要查看的盘符。然后显示该盘符的可用容量。
理论依据是:容量的单位是KB。再除1024就变成了MB。
代码如下:
Dim FSO As Object, DrvLetter As String, msg As String
Set FSO = CreateObject("Scripting.FileSystemObject")
DrvLetter = InputBox("查看的盘符:")
If DrvLetter = "" Then
Set FSO = Nothing
Exit Sub
End If
If FSO.DriveExists(DrvLetter) Then
msg = Format(FSO.GetDrive(DrvLetter).AvailableSpace / 1024, "#,##") & " KB"
MsgBox DrvLetter & "可用容量是、" & msg & "。", vbInformation
Else
MsgBox DrvLetter & "输入盘符不存在。", vbExclamation
End If
Set FSO = Nothing
End Sub