Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Zuerst muss ein Listview control erstellt und auf dem aktuellen Formular platziert werden.
Dim lv As New ListView
lv.Dock = DockStyle.Fill
Me.Controls.Add(lv)
'Nun wird die Listview konfiguriert.
With lv
'jetzt werden die Spalten und deren Beschriftungen zu dem Listview hinzugefügt.
'die Groesse spielt keine Rolle, da die groesse am Schluss der Function automatisch
'angepasst wird
.Columns.Add("Name", 200, HorizontalAlignment.Left)
.Columns.Add("Typ", 150, HorizontalAlignment.Left)
.Columns.Add("Groesse", 100, HorizontalAlignment.Right)
.Columns.Add("Freier Speicher", 100, HorizontalAlignment.Right)
.Columns.Add("Format", 100, HorizontalAlignment.Right)
.Columns.Add("Root", 100, HorizontalAlignment.Left)
'Die Spalten können währen der Laufzeit neu angeordnet werden
.AllowColumnReorder = True
'Detailansicht wählen
.View = View.Details
End With
'Gruppe "Hard Disk Drives" erstellen und hinzufügen
Dim hddGroup As New ListViewGroup("Hard Disk Drives", HorizontalAlignment.Left)
hddGroup.Name = "HDDGroup"
'Gruppe "Removable Storage" erstellen und Hinzufügen
Dim removableGroup As New ListViewGroup("Devices with Removable Storage", HorizontalAlignment.Left)
removableGroup.Name = "RemovableStorage"
'Nun werden die erstellen gruppen dem Listview hinzugefügt
lv.Groups.Add(hddGroup)
lv.Groups.Add(removableGroup)
'jetzt werden die Inhalte erstell
Dim li As ListViewItem
Dim volumeLabel As String = String.Empty
'EEs werden nun alle verfügbaren Lauf durchlaufen und entpsrechend zugeordnet
For Each driveName As System.IO.DriveInfo In My.Computer.FileSystem.Drives
li = New ListViewItem
If driveName.IsReady = False OrElse driveName.VolumeLabel.Length = 0 _
Then volumeLabel = driveName.DriveType.ToString Else volumeLabel = driveName.VolumeLabel
li.Text = volumeLabel & " (" & driveName.Name.Substring(0, 2) & ")"
li.SubItems.Add(driveName.DriveType.ToString)
Try
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add(FileSizer(driveName.TotalSize.ToString))
Catch ex As Exception
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add("n.a.")
End Try
Try
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add(FileSizer(driveName.TotalFreeSpace.ToString))
Catch ex As Exception
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add("n.a.")
End Try
Try
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add(driveName.DriveFormat.ToString)
Catch ex As Exception
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add("n.a.")
End Try
Try
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add(driveName.RootDirectory.ToString)
Catch ex As Exception
If driveName.IsReady = False Then li.SubItems.Add("0") Else li.SubItems.Add("n.a.")
End Try
'Type der Laufwerke erstellen und in die Gruppen zuordnen
Select Case driveName.DriveType
Case IO.DriveType.Fixed
li.Group = hddGroup
Case IO.DriveType.Removable, IO.DriveType.CDRom
li.Group = removableGroup
End Select
lv.Items.Add(li)
Next
'Größe der Spalten anpassen
For Each locColumn As ColumnHeader In lv.Columns
locColumn.Width = -2
Next
End Sub
'Rechnet in MB ... um
Private Function FileSizer(ByVal varSize As String) As String
Dim FileSize As Double
Dim size As String = " B"
FileSize = varSize
If FileSize / 1024 > 1 Then
FileSize /= 1024
size = " KB"
If FileSize / 1024 > 1 Then
FileSize /= 1024
size = " MB"
If FileSize / 1024 > 1 Then
FileSize /= 1024
size = " GB"
End If
End If
End If
FileSizer = Format(FileSize, "###0.0") & " " & size
Return FileSizer
End Function
End Class