Discussion Locked

Doubt in VB

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

How to write a Program to know about remaining free space in HardDisk in VB?

I want to know about free space available in Harddisk. How to write a program? Is there any function?

5 replies

by bill_brower

You can use the FileSystemObject in VB to get drive information. You must include a reference to the Microsoft Scripting Runtime in your project to use FSO. The following code was pulled from MSDN (Visual Basic Concepts book):

Private Sub Command3_Click()
Dim fso As New FileSystemObject, drv As Drive, s As String
Set drv = fso.GetDrive(fso.GetDriveName("c:"))
s = "Drive " & UCase("c:") & " - "
s = s & drv.VolumeName & vbCrLf
s = s & "Total Space: " & FormatNumber(drv.TotalSize / 1024, 0)
s = s & " Kb" & vbCrLf
s = s & "Free Space: " & FormatNumber(drv.FreeSpace / 1024, 0)
s = s & " Kb" & vbCrLf
MsgBox s
End Sub

by sl-campbell

This will return the number of free MBs:

Put in the declarations section of a standard module:

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

'add this function
Public Function FreeDiskSpace(sDrive As String) As Single
'Returns MBs Free

Dim lBytesPerSector As Long
Dim lSectorsPerCluster As Long
Dim lNumOfFreeClusters As Long
Dim lTotalNumOfClusters As Long

If GetDiskFreeSpace(Left$(sDrive, 1) + ":\", lSectorsPerCluster, lBytesPerSector, lNumOfFreeClusters, lTotalNumOfClusters) Then

FreeDiskSpace = lNumOfFreeClusters / 1000000000 * lSectorsPerCluster * lBytesPerSector
End If
End Function

Call like this:
Debug.Print FreeDiskSpace("C")

This discussion is closed to new replies.