robo100
Member
Is there an easy way to read free hard disk space? I have an application where I want to warn the user that the hard drive is getting full.
Dim disk As New ManagementObject("win32_logicaldisk.deviceid=""c:""")
disk.Get()
MsgBox(disk("FreeSpace").ToString())
Dim moSearch As New ManagementObjectSearcher("Select * from Win32_LogicalDisk")
Dim myCollection As ManagementObjectCollection = moSearch.Get()
Dim msg As String = ""
For Each WmiObject As ManagementObject In myCollection
If WmiObject("DriveType") = 3 Then 'local disk, non-removable
msg += WmiObject("DeviceID") _
& " " & WmiObject("FreeSpace") _
& " free of " & WmiObject("Size") & vbCrLf
End If
Next
MsgBox(msg)
Dim msg As String = ""
Dim fso As New Scripting.FileSystemObject
Dim drv As Scripting.Drive
For Each drv In fso.Drives
If drv.IsReady And drv.DriveType = Scripting.DriveTypeConst.Fixed Then
msg += drv.DriveLetter _
& " " & drv.FreeSpace _
& " free of " & drv.TotalSize & vbCrLf
End If
Next
MsgBox(msg)
Function GetDiskPctFree(ByVal lsDriveLtr As String) As Single
Static lnPctFree As Single = 0
Dim di As System.IO.DriveInfo
di = My.Computer.FileSystem.GetDriveInfo(lsDriveLtr)
If di.IsReady Then
If di.AvailableFreeSpace > 0 Then
lnPctFree = (di.AvailableFreeSpace / di.TotalSize) * 100
End If
End If
Return lnPctFree
End Function