Hey Guys, I am trying to create this application which can display the free space and the total space of logical drives,,I am very close but there is some little mistake which I am not being able to find out..Please help,,here is the code
Public Class Form2
Private strDrive As String = ""
Private lngFreeSpace As Double = 0
Private lngTotalSpace As Double = 0
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
FindDrives()
ComboBox2.SelectedIndex = 0
strDrive = ComboBox1.Text.Substring(0, 2)
'DriveInfoSystem info=DriveInfo.GetInfo(strDrive);
Dim info As DriveInfoSystem = DriveInfo.GetInfo("C:\Program Files")
lngFreeSpace = Convert.ToDouble(info.Available)
lngTotalSpace = Convert.ToDouble(info.Total)
Label5.Text = lngFreeSpace.ToString()
Label6.Text = lngTotalSpace.ToString()
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
Conversion()
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectionChangeCommitted
Conversion()
End Sub
Private Sub Conversion()
strDrive = ComboBox1.Text.Substring(0, 2)
Dim info As DriveInfoSystem = DriveInfo.GetInfo(strDrive)
lngFreeSpace = Convert.ToDouble(info.Available)
lngTotalSpace = Convert.ToDouble(info.Total)
Dim intIndex As Integer = ComboBox2.SelectedIndex
Select Case intIndex
Case 1
lngFreeSpace = lngFreeSpace / 1024
lngTotalSpace = lngTotalSpace / 1024
Exit Select
Case 2
lngFreeSpace = (lngFreeSpace / 1024) / 1024
lngTotalSpace = (lngTotalSpace / 1024) / 1024
Exit Select
Case 3
lngFreeSpace = ((lngFreeSpace / 1024) / 1024) / 1024
lngTotalSpace = ((lngTotalSpace / 1024) / 1024) / 1024
Exit Select
Case 4
lngFreeSpace = (((lngFreeSpace / 1024) / 1024) / 1024) / 1024
lngTotalSpace = (((lngTotalSpace / 1024) / 1024) / 1024) / 1024
Exit Select
End Select
Label5.Text = lngFreeSpace.ToString()
Label6.Text = lngTotalSpace.ToString()
End Sub
Private Sub FindDrives()
Dim tempString As String() = Directory.GetLogicalDrives()
For Each tempDrive As String In tempString
ComboBox1.Items.Add(tempDrive)
Next
ComboBox1.SelectedIndex = 0
End Sub
Public Structure DriveInfoSystem
Public ReadOnly Drive As String
Public ReadOnly Result As Long
Public ReadOnly Available As Long
Public ReadOnly Total As Long
Public ReadOnly Free As Long
Public Sub New(ByVal drive As String, ByVal result As Long, ByVal available As Long, ByVal total As Long, ByVal free As Long)
Me.Drive = drive
Me.Result = result
Me.Available = available
Me.Total = total
Me.Free = free
End Sub
End Structure
Public NotInheritable Class DriveInfo
Private Drive As String
Private Result As Long
Private Available As Long
Private Total As Long
Private Free As Long
Public Sub New(ByVal drive As String, ByVal result As Long, ByVal available As Long, ByVal total As Long, ByVal free As Long)
Me.Drive = drive
Me.Result = result
Me.Available = available
Me.Total = total
Me.Free = free
End Sub
<DllImport("kernel32.dll", EntryPoint:="GetDiskFreeSpaceExA")> _
Private Shared Function GetDiskFreeSpaceEx(ByVal lpDirectoryName As String, ByRef lpFreeBytesAvailableToCaller As Long, ByRef lpTotalNumberOfBytes As Long, ByRef lpTotalNumberOfFreeBytes As Long) As Long
End Function
Public Shared Function GetInfo(ByVal drive As String, ByRef available As Long, ByRef total As Long, ByRef free As Long) As Long
Return GetDiskFreeSpaceEx(drive, available, total, free)
End Function
Public Shared Function GetInfo(ByVal drive As String) As DriveInfoSystem
Dim result As Long, available As Long, total As Long, free As Long
result = GetDiskFreeSpaceEx(drive, available, total, free)
Return New DriveInfoSystem(drive, result, available, total, free)
End Function
End Class
End Class