|
|
Title | Display information about the computer's logical drives in VB.NET |
Description | This example shows how to display information about the computer's logical drives in VB.NET. The program uses the Directory.GetLogicalDrives method to get an array of the drive names and displays them in a ListBox. When the user clicks on a drive, it uses the File System Object's GetDrive method to get information about the selected drive. |
Keywords | drive, drives, list drives, logical drives, VB.NET |
Categories | Windows |
|
|
When it starts, the program uses the Directory.GetLogicalDrives method to get an array of the drive names and displays 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
|
|
When the user clicks on a drive, the program uses the File System Object's GetDrive method to get information about the selected drive. Note that the program checks the Drive object's IsReady property before it accesses the object's other properties.
|
|
' Display information about the selected drive.
Private Sub lstDrives_Click(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles lstDrives.Click
' Get the Drive object.
Dim fso As New FileSystemObject
Dim selected_drive As Drive = _
fso.GetDrive(lstDrives.SelectedItem.ToString)
' Display the drive's information.
Dim txt As String = ""
If selected_drive.IsReady Then
txt &= "Ready" & vbCrLf
txt &= "Drive Type: " & _
selected_drive.DriveType.ToString & vbCrLf
txt &= "Volume Name: " & _
selected_drive.VolumeName & vbCrLf
txt &= "Total Size: " & _
selected_drive.TotalSize.ToString & vbCrLf
txt &= "Available Space: " & _
selected_drive.AvailableSpace.ToString & vbCrLf
txt &= "Free Space: " & _
selected_drive.FreeSpace.ToString & vbCrLf
txt &= "File System: " & _
selected_drive.FileSystem & vbCrLf
txt &= "Serial Number: " & _
selected_drive.SerialNumber & vbCrLf
Else
txt &= "Not Ready" & vbCrLf
txt &= "Drive Type: " & _
selected_drive.DriveType.ToString & vbCrLf
txt &= "Volume Name: ?" & vbCrLf
txt &= "Total Size: ?" & vbCrLf
txt &= "Available Space: ?" & vbCrLf
txt &= "Free Space: ?" & vbCrLf
txt &= "File System: ?" & vbCrLf
txt &= "Serial Number: ?" & vbCrLf
End If
lblDriveInfo.Text = txt
End Sub
|
|
|
|
|
|