mario.deghetto

Visual Basic & .NET
posts - 332, comments - 79, trackbacks - 0

#32: Elencare hard disk con spazio libero disponibile

In realtà la tip che presento oggi elenca gli hard disk ma anche gli eventuali dischi di altro tipo che dovessero essere disponibili nel sistema. Dato che alcune unità possono essere al momento prive di disco (es. l’unità per floppy disk A:, unità con disco removibile, lettori CD/DVD ecc.), è necessario gestire l’eccezione che interromperebbe l’esecuzione del programma.

Per provare questa tip è sufficiente creare un form, aggiungere un pulsante e una TextBox, impostare quest’ultima a “MultiLine” e inserire il seguente codice:

Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button1.Click
      Dim driveInfo As System.IO.DriveInfo() = _
         System.IO.DriveInfo.GetDrives()
      For Each d As System.IO.DriveInfo In driveInfo
         Try
            TextBox1.Text += “Drive: “ + d.Name.ToString _
               + System.Environment.NewLine + “Size: “ _
               + d.TotalSize.ToString + System.Environment.NewLine _

               + “Free Space: “ + d.TotalFreeSpace.ToString _
               + System.Environment.NewLine + System.Environment.NewLine
         Catch ex As Exception
            ‘ ignora gli errori dovuti a dischi “non hard disk”
            ‘ es. A:\ oppure lettori CD/DVD e unità non pronte
            ‘ (senza disco)
         End Try
      Next
   End Sub
End Class

Ecco un esempio del risultato che possiamo ottenere:

Esempio tip #32

Print | posted on venerdì 15 gennaio 2010 5.35 |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 3 and type the answer here:

Powered by: