The following code snippets explains how to retrieve the logical drives in the system, information about the logical drives (Drive Name, Drive Type, Available Free Size, Volume Label etc.,) present in the system.
All we have to do is to reference the namespace System.IO
The System.IODirectory.GetLogicalDrives method is used to get an array of the drive names. I have displayed them in a ListBox.
' List the drives.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
For Each drive As String In Directory.GetLogicalDrives()
lstDrives.Items.Add(drive)
Next drive
End Sub
This code snippet how to retrieve information about each logical drive. The important thing to be noted here that, we have to check whether the device is ready before accessing it's properties, otherwise it will throw the exception "Device is not ready"
'List the Drives Information for each drive
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strInfo As String = ""
For Each drive_info As DriveInfo In DriveInfo.GetDrives()
If drive_info.IsReady() Then
' Display information about the selected drive.
strInfo = strInfo & " Device is ready" & vbCrLf
strInfo = strInfo & "Drive Name: " & drive_info.Name.ToString & Environment.NewLine
strInfo = strInfo & "Drive Type: " & drive_info.DriveType.ToString & Environment.NewLine
strInfo = strInfo & "Drive Format: " & drive_info.DriveFormat.ToString & Environment.NewLine
strInfo = strInfo & "Available Free Space: " & drive_info.AvailableFreeSpace().ToString & Environment.NewLine
strInfo = strInfo & "Total Free Space: " & drive_info.TotalFreeSpace.ToString & Environment.NewLine
strInfo = strInfo & "Total Size: " & drive_info.TotalSize.ToString & Environment.NewLine
strInfo = strInfo & "Volume Label: " & drive_info.VolumeLabel.ToString & Environment.NewLine
strInfo = strInfo & "Root Directory: " & drive_info.RootDirectory.ToString & Environment.NewLine
Else
' Display information about the selected drive.
strInfo = strInfo & " Device not ready" & vbCrLf
strInfo = strInfo & "Drive Name: " & drive_info.Name.ToString & Environment.NewLine
strInfo = strInfo & "Drive Type: " & drive_info.DriveType.ToString & Environment.NewLine
'strInfo = strInfo & "Drive Format: " & drive_info.DriveFormat.ToString & Environment.NewLine
'strInfo = strInfo & "Available Free Space: " & drive_info.AvailableFreeSpace().ToString & Environment.NewLine
'strInfo = strInfo & "Total Free Space: " & drive_info.TotalFreeSpace.ToString & Environment.NewLine
'strInfo = strInfo & "Total Size: " & drive_info.TotalSize.ToString & Environment.NewLine
'strInfo = strInfo & "Volume Label: " & drive_info.VolumeLabel.ToString & Environment.NewLine
strInfo = strInfo & "Root Directory: " & drive_info.RootDirectory.ToString & Environment.NewLine
End If
Next drive_info
lblDriveInfo.Text = strInfo 'Displaying the information in the label
End Sub
Finally display the information in the label.
No comments:
Post a Comment