Friday, November 9, 2012

Get All Drives from your computer

Get All Drives from your computer in VB.NET

Retrieve all your drives information easily simply by calling the GetDrive function. You also have to be careful with all the IN and OUT problems. Here is a sample with little protection to give you an idea:





Public Class Form1
    ''' <summary>
    ''' Sample : get all drives and get directories. very little error management
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>this is a sample, 2012-11-09</remarks>
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim AllDrive() As System.IO.DriveInfo
        Dim sDrives As String
        Dim sDirectory() As String

        Try
            sDrives = ""
            AllDrive = System.IO.DriveInfo.GetDrives 'get all drives availables on your computer
            For Each drive As System.IO.DriveInfo In AllDrive
                If drive.IsReady = True Then 'prevent crash : CD-ROM drive or disconnected drive
                    sDrives = sDrives & drive.Name & vbCrLf
                    sDirectory = System.IO.Directory.GetDirectories(drive.Name) 'get all drives Names

                    For Each sFolder As String In sDirectory
                        'TODO something here
                    Next
                End If
            Next
            MsgBox(sDrives) 'display all available drive Names
        Catch ex1 As System.IO.IOException
            MsgBox(ex1.StackTrace) 'basic error message for read-write-files-directories....
        Catch ex As Exception
            MsgBox(ex.StackTrace) 'basic error message
        End Try

    End Sub
End Class



If you run the little program, a MsgBox will display all the available drives. I also introduce the IsReady property in case you want to access one of those drives.
If you try to access a CD-Rom drive (DVD, SD drive, floppy drive A: …). What I just said??? Floppy drive?  He! He! You will also need to be careful if you try to access restricted folder.  The folder “Documents and Setting” on your “C:” might not be accessible from your little program. So you have to work harder.



Reference :

The program I bought for this article: Visual Studio 2010 Professional (Old Version)
A newer version for Visual Basic : Microsoft Visual Studio Pro 2012
Check Technologies Officiel Web Site : http://checktechno.ca


No comments:

Post a Comment