In questo esempio vedremo come ottenere informazioni sulle unità usando vb.net 2005
Innanzi tutto riempiamo una combobox con tutte le unità presenti che vanno dalla “a” alla “z”
Inseriamo nell’evento form_load() il codice seguente.
Prima di tutto però dichiariamo le seguenti variabili
Private dirInfo As DirectoryInfo
Private totalSpace As Long
Private freeSpace As Long
Private usedSpace As Long
Private sweep As Single
Private isSpaceInfoAvailable As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.driveReadyStatus.Text = ""
‘Ricava le unità presenti nel sistema
Dim drives As System.IO.DriveInfo() = System.IO.DriveInfo.GetDrives
‘Popola la combobox con le lettere delle unità
drivesOnPc.Items.AddRange(drives)
End Sub
Per abbellire il progetto faremo in modo che le informazioni come spazio totale sul disco , spazio usato ecc.. siano rappresentati graficamente ecco il codice che segue….
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Reattangolo che definirà il Pie Chart
Dim rect As Rectangle = New Rectangle(370, 20, 200, 200)
Dim rect2 As Rectangle = New Rectangle(310, 10, 320, 320)
' Rettangoli che verranno usati per la legenda
Dim freeLegend As Rectangle = New Rectangle(315, 275, 20, 20)
Dim usedLegend As Rectangle = New Rectangle(315, 300, 20, 20)
e.Graphics.DrawRectangle(Pens.Black, rect2)
If isSpaceInfoAvailable = True Then
' Disegna Pie Chart
e.Graphics.FillPie(Brushes.Green, rect, 0, sweep)
e.Graphics.FillPie(Brushes.Red, rect, sweep, 360 - sweep)
' Disegna la legenda
e.Graphics.FillRectangle(Brushes.Green, freeLegend)
e.Graphics.FillRectangle(Brushes.Red, usedLegend)
' Inserisce il testo
e.Graphics.DrawString("Capacity:", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(350, 230))
e.Graphics.DrawString("Used Space:", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(335, 275))
e.Graphics.DrawString("Free Space:", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(335, 300))
e.Graphics.DrawString(totalSpace.ToString("N") + " bytes", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(420, 230))
e.Graphics.DrawString(usedSpace.ToString("N") + " bytes", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(420, 275))
e.Graphics.DrawString(freeSpace.ToString("N") + " bytes", New Font("Tahoma", 10, FontStyle.Regular), Brushes.Black, New PointF(420, 300))
End If
End Sub
Finalmente siamo arrivati alla classe che farà funzionare l’intero progetto
La seguente classe viene usata per ricavare informazioni delle unita.
Useremo la classe System.IO.DriveInfo(LetteraUnita) per acquisire info sull’unita selezionata.
Private Sub LoadDriveInfo(ByVal driveLetter As String)
Dim driveInfo As System.IO.DriveInfo
'Controlla che l'unita selezionata sia valida
Try
driveInfo = New System.IO.DriveInfo(driveLetter)
Catch ex1 As ArgumentNullException
MessageBox.Show("L'unita selezionata non è valida./n/r" + ex1.Message, "Unita non valida", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
Catch ex2 As ArgumentException
MessageBox.Show("The drive letter must be in the range of a-z./n/r" + ex2.Message, "Drive Letter error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Me.driveName.Text = driveInfo.Name
'Alcune unita non mostrano tutte le info
Try
If driveInfo.VolumeLabel.Length > 0 Then
Me.driveVolumeLabel.Text = driveInfo.VolumeLabel
Else
Me.driveVolumeLabel.Text = "None"
End If
Me.driveFormat.Text = driveInfo.DriveFormat
totalSpace = driveInfo.TotalSize 'Dimensione dell'unita
freeSpace = driveInfo.TotalFreeSpace 'Spazio non usato
usedSpace = totalSpace - freeSpace 'Ricava lo spazio usato
sweep = 360.0F * freeSpace / totalSpace
isSpaceInfoAvailable = True
Catch
Me.driveVolumeLabel.Text = "Not available"
Me.driveFormat.Text = "Not available"
isSpaceInfoAvailable = False
End Try
Me.driveType.Text = driveInfo.DriveType.ToString
Me.driveRootDirectory.Text = driveInfo.RootDirectory.ToString
dirInfo = driveInfo.RootDirectory
If driveInfo.IsReady = True Then
Me.driveReadyStatus.Text = "Drive is Ready"
Else
Me.driveReadyStatus.Text = "Drive is NOT Ready"
End If
End Sub
Le classi seguenti non fanno altro che convertire i bytes in mb e in gb.
Private Function ConvertBytesToMB(ByVal bytes As Int64) As String
Dim mb As Long = bytes / 1048576
Return mb.ToString("N")
End Function
Private Function ConvertBytesToGB(ByVal bytes As Int64) As String
Dim gb As Long = bytes / 1073741824
Return gb.ToString("N")
End Function